title: From char* to stream
body: <p>I want to use Byte Pair Encoding to decompress a byte array. The sourcecode (not from me) I have is using filestream to read a file byte by byte. However, I want to decompress <code>char *</code>.</p>
  
  <p>I have been trying to convert this using stringstream and other things, but I can't figure out how to do this.</p>
  
  <p>I would like to use it like this: <code>expand(char *inputarray, char *outputarray)</code></p>
  
  <p>I am new to c++, just switched from vb.net, so don't be too hard on me :)</p>
  
  <p>This is the code:</p>
  
  <pre><code>/* expand.c */
  /* Copyright 1994 by Philip Gage */
  
  #include &lt;stdio.h&gt;
  
  /* decompress data from input to output */
  void expand (FILE *input, FILE *output)
  {
    unsigned char left[256], right[256], stack[30];
    short int c, count, i, size;
  
    /* unpack each block until end of file */
    while (( count = getc ( input )) != EOF )
    {
      /* set left to itself as literal flag */
      for ( i = 0 ; i &lt; 256; i++ )
      {
        left[i] = i;
      }
  
      /* read pair table */
      for ( c = 0 ; ; )
      {
        /* skip range of literal bytes */
        if ( count &gt; 127 )
        {
          c += count -127;
          count = 0;
        }
        if ( c==256 )
        { 
          break;
        }
  
        /* read pairs, skip right if literal */
        for ( i = 0; i &lt;= count; i++, c++ )
        {
          left[c] = getc(input);
          if ( c != left[c] )
          {
            right[c] = getc(input);
          }
        }
        if (c == 256)
        {
          break;
        }
        count = getc(input);
      }
  
      /* calculate packed data block size */
      size = 256 * getc(input) + getc(input);
  
      /* unpack data block */
      for ( i = 0 ; ; )
      {
        /* pop byte from stack or read byte */
        if ( i )
        { 
          c = stack[--i];
        }
        else
        {
          if ( !size--)
          {
            break;
          }
          c = getc(input);
        }
  
        /* output byte or push pair on stack */
        if ( c == left[c] )
        {
          putc(c, output);
        }
        else
        {
          stack[i++] = right[c];
          stack[i++] = left[c];
        }
      }
    }
  }
  
  void main ( int argc, char *argv[] )
  {
    FILE *infile, *outfile;
  
    if ( argc != 3 )
    {
      printf("Usage: expand infile outfile\n");
    }
    else
    {
      if (( infile = fopen(argv[1],"rb"))==NULL)
      {
        printf("Error opening input %s\n",argv[1]);
      }
      else
      {  
        if ((outfile=fopen(argv[2],"wb"))==NULL)
        {
          printf("Error opening output %s\n", argv[2]);
        }
        else
        {
          expand ( infile, outfile );
          fclose ( outfile );
          fclose ( infile );
        }
      }
    }
  }
  
  /* end of file */
  </code></pre>