LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get size of buffer necessary to ReadFile

I am simply trying to import a text file into a text box in CVI 6.0 under XP.
What I did is GetFileSize , Allocate a buffer, OpenFile , Read file , SetCtrlVal of the text field to the buffer and close the file. The Problem is I am reading in junk at the end of the file and the bigger the file the larger is the junk , this junk is a repetition of part of the data of the file itself.


ResetTextBox (HelperWindow[1] , GCODEPANEL_GCODE_EDIT ," " );

GetFileSize (Filename, &Buffersize);
Buffersize++; // add one for /0 terminator
ChBuffer =calloc (Buffersize,sizeof(char));
FileHandle = OpenFile (Filename, VAL_READ_WRITE, VAL_OPEN_AS_IS, VAL_ASCII);

result = ReadFile (FileHandle, ChBuffer, Buffers
ize);

SetCtrlVal (HelperWindow[1] , GCODEPANEL_GCODE_EDIT ,ChBuffer);

result = CloseFile (FileHandle);


I tried making my own GetFilesize that counts characters till feof and it was correct but slow a 2Mb file take 10 seconds to measure . I hence beleive some data buffer is attached by windows after the eof marker but I wouldn't know a fast way to measure the useful part of a text file.This effect happens on all computers I tried , and it can be seen on small files.

Thanks David
0 Kudos
Message 1 of 4
(3,480 Views)
There are (at least) two tricks found in the on-line help for ReadFile():
One trick is that "ReadFile does not terminate the buffer with an ASCII NUL.". You correctly compensated for that by
Buffersize++; // add one for /0 terminator
before calloc().
The other trick is the solution to your problem:
"If you open the file in ASCII mode, ReadFile counts each CR LF combination read as one character, because the pair is translated into LF when ReadFile stores it in the buffer." But if you asked to read the number of characters specified by GetFileSize(), ReadFile still has characters to read after it reaches the end of the file, so it then seems to switch back to binary mode file pointer (CR+LF = 2 characters) and re-read the end of the file.
You can fix it easily in one
of two ways: either read the file in the Binary mode (VAL_BINARY) and read CR + LF as two characters, or truncate the buffer based on the return value of ReadFile (the number of characters read).
result = ReadFile (FileHandle, ChBuffer, Buffersize);
ChBuffer[result] = '\0';
0 Kudos
Message 2 of 4
(3,480 Views)
Maybe ReadFile doesn't re-read the end of the file: maybe if reads it first in the binary mode (CR + LF = two characters), and then rewrites it in the ASCII mode (as ReadFile defines it, CR + LF = LF). Then the problem really is that "ReadFile does not terminate the buffer with an ASCII NUL.", so you have to, based on the new character count in the (unterminated) buffer. ReadFile provides the new character count in its return value.
0 Kudos
Message 3 of 4
(3,480 Views)
Thanks for your reply .
Meanwhile I discovered the ASCII thing but your explanation was very useful !
0 Kudos
Message 4 of 4
(3,480 Views)