03-09-2015 04:05 PM
Try to read a binary file.
OpenFile with VAL_BINARY. file handle returned is NOT -1 (is 3).
ReadFile returns 4, but the string is empty.
iFileHandle = OpenFile(szPath, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_BINARY);
if (iFileHandle < 0)
err = ERROR_FILE_NOT_FOUND;
SetFilePtr(iFileHandle, 0, 0);
nSize = ReadFile(iFileHandle, szSize, 4);
Wher nSize is 4, but szSize is empty.
StringLength is 0
The first byte is NULL. That might be the problem.
How do I get around this ?
TIA.
Solved! Go to Solution.
03-09-2015 05:16 PM - edited 03-09-2015 05:17 PM
If the first byte in the file is NULL then the resulting string both if printed with printf or viewed in the debug view will be empty. That's how C interprets end of string.
In debug view, to see all the bytes read from the file show the string then goto options 》show entire string and you'll see the whole memory area assigned to it, the first four bytes of it read from the file. I'm writing from a tablet with no cvi installed so the menu option may appear slightly different.
03-09-2015 05:23 PM
Thanks for the quick response.
I found that out too after I made the post.
Now the problem is ReadFile doesn't move the file position pointer correctly on loop index 1.
Have seen this before ?
03-09-2015 06:37 PM
Is there any reason to use ReadFile and Co instead of the standard C functions ? The capabilities are the same (as far as I remember) and they are not much more convenient. And the day you want to move your code to a different environment, at least you can still compile it...
03-10-2015 02:44 AM
gdargaud spotted a good suggestion in the direction of portable code.
If you want to continue using CVI functions, check that you have not some SetFilePtr left in the loop: the file pointer is automatically moved on every read.
03-10-2015 10:41 AM
Problem solved.
ReadFile moves the file pointer correctly for binary file.
It was the source file has problem.