LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read a binary file

    I'm reading a binary file(see attached temp204.txt) by using ReadFile()function. I use ultraedit software to open this binary file and show it with Hex format, please see attached screenshot.gif. As you can see in the screenshot.gif, there contains NULL(0x00) in certain line like "20 7c ff ff 9e 30 22 7c ff ff 9e 60 10 bc 00 e0", my issue is when using ReadFile(), it's will always read the characters only before 00, that is to say, it will only read "20 7c ff ff 9e 30 22 7c ff ff 9e 60 10 bc", not "20 7c ff ff 9e 30 22 7c ff ff 9e 60 10 bc 00 e0". How can I resolve this kind of problem?
 
Thanks!
Jacky
Download All
0 Kudos
Message 1 of 7
(4,497 Views)
You need to use OpenFile() with the VAL_BINARY file type specifier. This will allow ReadFile() to read in raw binary data, instead of stopping at an ASCIIZ terminator.
 
JR
0 Kudos
Message 2 of 7
(4,488 Views)
Actually, I have the same thing as you suggested but indeed, it will stop when encounting "00".

-------------------------
fhandle=OpenFile ("temp204.dat", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_BINARY);
i=ReadFile (fhandle, temp, 100);
---------------------

tHANKS!
Jacky
0 Kudos
Message 3 of 7
(4,481 Views)
If you are sure that ReadFile() is not returning the correct number of bytes (which you can check with your variable i in your sample code above), and that the problem is not simply that subsequent code handling functions (including CVI debug windows) are viewing data as ASCII and thus they are stopping at the 00, then perhaps you should give the ANSI C equivalent functions a try. Personally, I always use ANSI C functions when they are available. Much more predictable and portable. (Look for fopen(), fread() and fclose() for starters.)
 
JR
0 Kudos
Message 4 of 7
(4,475 Views)
You are right, I think the data from ReadFile() is just raw data, I need to do more. I know I can refer the codes like below, however, I beg someone's help on how to write in detail, especially on use of Scan function. 
 
Thanks!
Jacky
 
 GetFileSize (file, &size);
 nel = (int)size / (int)sizeof (int);
 data = calloc (nel, sizeof (int));
 raw = malloc (size); memset (raw, 0, size);
 fH = OpenFile (file, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_BINARY);
 ReadFile (fH, raw, size);

 Scan (raw, "%*d[b2]", nel, data);
 for (i = 0; i < nel; i++) data[i] = SwapBytes (raw + i * 4); 
 
 
int SwapBytes (char *src)
// Conversione di numero reale dal formato del plc a FLOAT
{
 char tmp[4];
 tmp[0] = *(src + 3);
 tmp[1] = *(src + 2);
 tmp[2] = *(src + 1);
 tmp[3] = *(src + 0);
 return *(float *)tmp;
}

 
0 Kudos
Message 5 of 7
(4,456 Views)

One of your problems is that Scan() is a string based function, so it will stop on the 00. If I understand your application correctly, you do not need to use this function at all (it is designed to read/convert ASCII strings, which you do not have); your binary data is already present in an array (raw) so you can simply access the elements directly.

I cannot translate the comments in your function SwapBytes(), but I suspect you might want to have another look at it to see if it should be a float function, not an int one? Similarly for your data array of ints - are you trying to store floats in it?

JR

0 Kudos
Message 6 of 7
(4,444 Views)

Thanks, and I got it. I use ScanFile() and it works,

 

 

Thanks!

Jacky

0 Kudos
Message 7 of 7
(4,443 Views)