problem reading and outputting binarry file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imarinoff
    New Member
    • Nov 2006
    • 2

    problem reading and outputting binarry file

    Hi

    I have a problem with the following code.

    #include <iostream>
    #include <fstream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    int i;
    int me;
    char * Buffer;
    ifstream inFile (argv[1], ios::binary );

    if (!inFile) {
    cerr << "Unable to open image file";
    exit(1);
    }

    int myBuffLen;


    inFile.seekg (0, ios::end);
    myBuffLen = inFile.tellg();
    inFile.seekg (0, ios::beg);

    Buffer = new char [myBuffLen];



    inFile.read(Buf fer, myBuffLen);
    cout << Buffer;


    inFile.close();
    free(Buffer);

    return 0;
    }


    A very simple code for reading binarry files but when I run the problem it prints only "ÿØÿáøExifo f" when i set the file to be an image jpeg file.

    Any one can help?
    Thanks in advance!
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    The buffer has correct value only.
    To check that I used the output file.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       char * 
          pszBuffer = NULL;
       ifstream 
          inFile (argv[1], ios::binary);
       ofstream
          outFile;
       int 
          nBufferLength = 0;
    
       if (!inFile.is_open()) 
       {
          cerr << "Unable to open image file " << argv[1];
          exit(1);
       }
    
       // get the length of the file.
       inFile.seekg (0, ios::end);
       nBufferLength = inFile.tellg();
       inFile.seekg (0, ios::beg);
    
       // allocate the memory.
       pszBuffer = new char [nBufferLength + 1];
       if(pszBuffer == NULL)
       {
          cerr << "Failed to allocate the memory.";
          exit(1);
       }  
       
       // Fill the memory with the zero.
       memset(pszBuffer, 0, nBufferLength);
    
       // read the characters.
       inFile.read(pszBuffer, nBufferLength);
       cout << pszBuffer;
    
       outFile.open("out.bmp", ios::binary);
       if (inFile.is_open() == true) 
       {
          outFile.write(pszBuffer, nBufferLength);
       }
       else
       {
          cerr << "Unable to open the output file " << argv[1];
       }
    
       outFile.close();
       inFile.close();
       if(pszBuffer != NULL)
       {
          delete []pszBuffer;
          pszBuffer = NULL;
       }
       
       // Note : When you allocate a memory using new you must free the memory using the delete only.
       //        Not using free method. If you want to use free method, allocate the memory using
       //        malloc or calloc.
       // free(pszBuffer);
    
       return 0;
    }
    Regards,
    M.Sivadhas.

    Comment

    • imarinoff
      New Member
      • Nov 2006
      • 2

      #3
      why the buffer cannot be printed correctly to cout?
      The example you gave outs the imge in file in the proper way but it does not print the buffer to the default stdout correctly.

      I want to use this problem as cgi binary and the goal is the image to be printed in the browser. Now in the browser I got a broken image and the out.bmp file is OK.

      Comment

      Working...