istream::read issue

Hi All,

I have written a small piece of code which reads a file and prints it on the console. I compiled the file both in windows and linux and found that in windows the istream::read encounters the EOF character before then that of Linux.

Here is the code:

// load a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char*argv[])
{
int length;
char * buffer;

ifstream is("CT-MONO2-12-lomb-an2.dcm");

// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);

cout <<"File Length: " << length << endl;
buffer = (char*) malloc(length);
for(int j =0; j < length ;j++)
{
is.read(buffer,1);
int test = is.peek();
cout << i << " : " << test << endl;
if(is.peek() == EOF){
cout << "Encountered end of file: at position"<< i<<endl;
}
if(failStatus)
break;
i++;
}
cout << "Bytes Read: " << i << endl;
is.close();
free(buffer);
return 0;
}

FOR WINDOWS: Encountered the end at 12501
FOR LINUX: Encountered the end at 525517

Please let me regarding this behaviour of WINDOWS when the file size is 525518.

Thanks
Santosh S.B






In Windows, you need to open the file in in binary mode (std::fstream is("CT-MONO2-12-lomb-an2.dcm", std::ios::binary);).
Topic archived. No new replies allowed.