LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ReadFile to get EOF

Solved!
Go to solution

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));

0 Kudos
Message 1 of 2
(3,757 Views)
Solution
Accepted by topic author sonotk

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;

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 2
(3,743 Views)