473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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(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
2 1359
sivadhas2006
142 100+
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
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

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

Similar topics

2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
3
by: Ron L | last post by:
I have an application that I am working on with 2 other developers. The applciation uses Remoting for our calls to SQL Server. We have been developing and testing against our development machines...
8
by: Richard Schulman | last post by:
The following program fragment works correctly with an ascii input file. But the file I actually want to process is Unicode (utf-16 encoding). The file must be Unicode rather than ASCII or...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.