ifstream::tellg() error

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I am reading in image files in a program and I read in
the header in ascii mode and the data in binary mode.
The problem is, sometimes tellg() gives me a completely
incorrect result and sometimes it is just fine. It is
quite annoying because there is no other good way to read
these file and I think it is a problem with Visual C++,
since it works just fine when I compile it using g++ on a
Linux system. Here is an example:

char input[200];
std::ifstream fin("blocks.pgm", std::ios::in);
std::ifstream::pos_type position = fin.tellg();
fin>>input;
position = fin.tellg();

When I run this code, input gets the first two bytes of
the file as it should. The first few bytes of the file
are: 'P5 302 274 255 '. Now, even though it only read
two bytes into input, when tellg() is called on the next
line it returns a position of 3586 bytes into the file.

If I change the input file to "original.pgm", a different
image file, everything works fine. The first 15 bytes
are identical and after the first two bytes are read,
tellg() returns a position of 2 bytes into the file.

If I delete everything in blocks.pgm after the first 15
bytes it seems to work just fine. I don't understand how
bytes that should not be read could be affecting the tellg
() function.

Chris
 
Chris said:
I am reading in image files in a program and I read in
the header in ascii mode and the data in binary mode.
The problem is, sometimes tellg() gives me a completely
incorrect result and sometimes it is just fine. It is
quite annoying because there is no other good way to read
these file and I think it is a problem with Visual C++,
since it works just fine when I compile it using g++ on a
Linux system. Here is an example:

char input[200];
std::ifstream fin("blocks.pgm", std::ios::in);

You're not using standard IOStreams, but anyway - add std::ios::binary to
the flags.

-cd
 
Back
Top