LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ReadFile return empty string

Solved!
Go to solution

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.

 

 

George Zou
0 Kudos
Message 1 of 6
(4,922 Views)
Solution
Accepted by topic author zou

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.



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 6
(4,913 Views)

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 ?

 

 

George Zou
0 Kudos
Message 3 of 6
(4,910 Views)

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...

0 Kudos
Message 4 of 6
(4,905 Views)

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.



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?
0 Kudos
Message 5 of 6
(4,888 Views)

Problem solved.

ReadFile moves the file pointer correctly for binary file.

It was the source file has problem.

 

George Zou
0 Kudos
Message 6 of 6
(4,869 Views)