03-11-2013 02:26 PM
I am using ReadFile function to read in ASCII mode. ReadFile counts CR+LF as one LF.
The byteRead are always less that the fileSize which is obtained using GetFileInfo(). Therefore the data read is not till the end of file.
The loop in this case never exit as there is always few bytes to read due to <CR><LF>.
How can I use ReadFile to read the entire contents of file in ASCII mode or set the pointer to consider <CR><LF> as two bytes?
//Get file size
status = GetFileInfo (*patternFilePath, (ssize_t *)fileSize);
//Open file
localFileHdl = OpenFile (*patternFilePath, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
char *nextPositionToWrite = *patternFileContents;
size_t leftOverToRead = (size_t)*fileSize;
do {
//Read file
bufLen = ReadFile (localFileHdl, nextPositionToWrite, leftOverToRead);
leftOverToRead = leftOverToRead - bufLen;
nextPositionToWrite = nextPositionToWrite + bufLen;
} while(leftOverToRead > 0);
//Close file
errChk( CloseFile (localFileHdl));
Solved! Go to Solution.
03-12-2013 01:15 AM - edited 03-12-2013 01:24 AM
A single ReadFile like yours actually reads the entire file contents, provided the buffer is great enough, no matter what the return count of the call.
The return count plays only if you were reading the file in a loop: in this case, since ReadFile treats CR+LF pair as a single character and accordingly does not count the CR, the code should work with
leftOverToRead -= bufLen + 1;