LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a difference in byte count between ReadFile() and SetFilePointer() ?

I have seen in the CVI help that the ReadFile() command counts CR-LF combination as 1 character. When I want to set the file pointer with SetFilePointer(), it seems to count CR-LF as 2 characters.
Anyone noticed this and how may I fix the resulting shift in my code?
0 Kudos
Message 1 of 5
(3,429 Views)
If you want to use the ReadFile return value (Bytes Read) to set the file pointer later, try opening the file for binary reading instead of ASCII. If you're using OpenFile, use VAL_BINARY instead of VAL_ASCII.
ReadFile seems to behave strangely when you open a file for ASCII reading, as you correctly noticed. The other detail is that when it's reading the file, it counts CR-LF as two characters to reach the specified Count, then it translates the CR-LF to LF and it returns the (lower) count of characters after the translation.
Also remember that ReadFile doesn't add a NULL at the end of the buffer it reads. You'll have to do that based on the ReadFile return value.
If you want to read the file sequentially, you don't need to keep track of the file pointer: Read
File will just continue from where it left off.
0 Kudos
Message 2 of 5
(3,428 Views)
Thank you for your answer. I need to use this file in ASCII mode because it's an editable script that I need to parse character after character. I use a file pointer that I increment each time I read a byte.
I don't need the NULL terminator as I read only one byte each time, searching for C-style comment delimiters and other special chars I have defined.
Maybe there is an easier/cleaner way to develop such a parser?
0 Kudos
Message 3 of 5
(3,429 Views)
It sounds like we need to back up a little. I'm going to make some assumptions on your application.
Assumption: If you're reading an ASCII script file, you'll probably want to read it sequentially: that is, start at the top and read it in contiguous chunks toward the bottom.
Comment: Any of the CVI or ANSI C functions that are typically used to read from files (ReadFile, fread, fgets, fscanf, etc.) advance the file pointer to the next unread byte in the file. If you're reading a file sequentially, you do not need to manipulate the file pointer. When you open the file, the pointer is at the top of the file. As you read through the file, the file pointer is automatically advanced with each successful read. You don't need to call fsetpos or SetFile
Ptr if you're sequentially reading a file.
Assumption: Your script file is organized into lines.
Suggestion: why not read it as a line? The ANSI C function fgets() reads one line at a time. You don't have to try to figure out where the line ends, the function does it for you and returns the entire line in a string. If the format of each line is identical, you could use fscanf() to read and parse the lines. Reading the file line by line and parsing each line is faster than reading the file character by character.
Comment: If you open a file for binary reading, you can still read the data as ASCII. Binary reads just remove all filters between you and the data (like converting CR-LF to LF).
Comment: While parsing your script lines, use some of the ANSI C search functions, e.g. strstr() to search for a string or strchr to search for a character. These functions will be much more efficient than writing your own function.
0 Kudos
Message 4 of 5
(3,428 Views)
I needed to read char after char because C-Style comments (/*...*/) can start in the middle of a line and end in the middle of an other one.
I have to read and store commands from the file to an array but in the same time I must check for a matching pattern, and move back if the pattern is not complete (ex: SetVoltage(x/y); I check if the '/' is starting a comment ('/*'), and go back to store this char as a part of the command.
I finally decided to add 1 more char to the pointer each time a LF is seen, to count the CR, and it's working well.

Thank you very much for your explanations and answer anyway,

jm
0 Kudos
Message 5 of 5
(3,428 Views)