rKarthik

I generated a binary file with all ascii characters from 0 to 127. When I try to open the document using ReadFile() function, it does not read beyond 26 characters. I did some debugging and found that the 26th ascii character is actually the SUB char(Ctrl Z) which I guess is treated as EOF and so ReadFile stops reading the file.

When I tried opening the same file using ifstream, it works fine. Does this mean that ifstream interprets the same character differently.

Thanks,

KR




Re: Visual C++ General ReadFile() doesnt read the entire file

Mike Danes

Are you talking about the Win32 API ReadFile function That one does not do any sort of control character processing (or any other processing), it is always working in "binary" mode so it should not stop (and it does not, I checked) at EOF character. fread CRT function stops at EOF and so does ifstream (at least in the case where you check the result of the eof() ifstream member function).



Re: Visual C++ General ReadFile() doesnt read the entire file

rKarthik

I am new to C++. I was debugging the fread CRT function and it was invoking ReadFile() which was truncating at the EOF character. I was guessing that this was the Win32 ReadFile func (not sure).

So is that a difference between ifstream and fread in the sense that fread automatically checks for EOF and stops if it encounters one but for ifstream, we need to check it.

Thanks,
KarthikR





Re: Visual C++ General ReadFile() doesnt read the entire file

Mike Danes

"fread automatically checks for EOF and stops if it encounters one but for ifstream, we need to check it"

Actually ifstream checks for EOF too. If you try to read from ifstream after the EOF character it won't work. In fact ifstream uses fopen/fread/fwrite/fclose under cover so it inherits the same behavior.

If you don't want this to happen (stop at EOF) you need to open the file in binary mode:

fopen("d:\\test.bin", "rb");

or

ifstream s("d:\\test.bin", ios_base::binary | ios_base::read);

As for ReadFile it does not process EOF or any other special characters. It's the fread function that does this.





Re: Visual C++ General ReadFile() doesnt read the entire file

rKarthik

Thanks Mike. I guess that explains it.

KR





Re: Visual C++ General ReadFile() doesnt read the entire file

andi17

I have been using ReadFile to read a COM port (where the buffer is written to a file afterwards) and have been sending binary values from a microcontroller. The first byte is 0x00, and all bytes are read, but the buffer (and file) is empty at the end. I tested this by adding 0x30 to all bytes sent from the uC and I receive values in the buffer, so all I can think of is that values such as 0x00 (ascii NULL) are returning as EOF and so I get nothing in the file.

I know that it has been said that ReadFile reads binary, but from experience I have had problems with this, and am not sure how to fix it so that I can receive all values.