By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,345 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,345 IT Pros & Developers. It's quick & easy.

problem reading and outputting binarry file

P: 2
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(Buffer, 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 "˙Ř˙ářExifof" when i set the file to be an image jpeg file.

Any one can help?
Thanks in advance!
Nov 17 '06 #1
Share this Question
Share on Google+
2 Replies


sivadhas2006
100+
P: 142
Hi,

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.    char * 
  10.       pszBuffer = NULL;
  11.    ifstream 
  12.       inFile (argv[1], ios::binary);
  13.    ofstream
  14.       outFile;
  15.    int 
  16.       nBufferLength = 0;
  17.  
  18.    if (!inFile.is_open()) 
  19.    {
  20.       cerr << "Unable to open image file " << argv[1];
  21.       exit(1);
  22.    }
  23.  
  24.    // get the length of the file.
  25.    inFile.seekg (0, ios::end);
  26.    nBufferLength = inFile.tellg();
  27.    inFile.seekg (0, ios::beg);
  28.  
  29.    // allocate the memory.
  30.    pszBuffer = new char [nBufferLength + 1];
  31.    if(pszBuffer == NULL)
  32.    {
  33.       cerr << "Failed to allocate the memory.";
  34.       exit(1);
  35.    }  
  36.  
  37.    // Fill the memory with the zero.
  38.    memset(pszBuffer, 0, nBufferLength);
  39.  
  40.    // read the characters.
  41.    inFile.read(pszBuffer, nBufferLength);
  42.    cout << pszBuffer;
  43.  
  44.    outFile.open("out.bmp", ios::binary);
  45.    if (inFile.is_open() == true) 
  46.    {
  47.       outFile.write(pszBuffer, nBufferLength);
  48.    }
  49.    else
  50.    {
  51.       cerr << "Unable to open the output file " << argv[1];
  52.    }
  53.  
  54.    outFile.close();
  55.    inFile.close();
  56.    if(pszBuffer != NULL)
  57.    {
  58.       delete []pszBuffer;
  59.       pszBuffer = NULL;
  60.    }
  61.  
  62.    // Note : When you allocate a memory using new you must free the memory using the delete only.
  63.    //        Not using free method. If you want to use free method, allocate the memory using
  64.    //        malloc or calloc.
  65.    // free(pszBuffer);
  66.  
  67.    return 0;
  68. }
  69.  
  70.  
Regards,
M.Sivadhas.
Nov 17 '06 #2

P: 2
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.
Nov 17 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.