05-09-2012 06:59 AM
I have written an array of number in 2 column groups to a file using the LabWindows/CVI function ArrayToFile...Now if I want to read the file with FileToArray Function then how do I know the number of items in the file. during the write time I know how many array items to write. but suppose I want the file to read at some later time then How to find the number of items in the file,So that I can read the exact number and present it. Thanks to all
Solved! Go to Solution.
05-09-2012 07:07 AM
This involves two steps:
- determine the number of lines, i.e. count the number of line feeds. There are several options for this, e.g. use fgets or ReadLine functions
- analyze the line (buffer) for the number of items per line, using some scanning function such as sscanf; asuming a regular file structure this needs to be done only for the first line
An alternative is establishing your own file file format, where the first line contains the number of lines, rows, items, etc. Then you would only need to read and parse the first line.
05-09-2012 08:00 AM
For binary format:You can get size of file by FileExist() function quickly, and then calculate numberOfElements=FileSize/ElementSize; (Element can be obtained by create file with only one element)
For text file format , there is no easy way but you can use tips from wolfgang post
05-10-2012 01:27 AM
can you please explain how do I get the number of LineFeed using ReadLine()....should I check for the EOF. and also I am confused about the where the function stores the number of LineFeeds.....I am using like this
Bytes = ReadLine (file, nLine, 1);
Bytes: I know its the number of bytes in a Line
confused about the second argument!
Thanks.
05-10-2012 01:43 AM - edited 05-10-2012 01:44 AM
What about:
OpenFile ( your file ); cnt = 0; while ((br = ReadLine ( ... )) != -2) { if (br == -1) { // I/O error: handle it! break; } cnt++; } CloseFile ( ... );
There are some ways to improve performance of this code, but if you are not reading thousands of lines it's quite fast.
After this part you can dimension the array to pass to FileToArray... unless you want to read it yourself since you already have it open!
05-10-2012 01:47 AM
Hi,
I am starting with the second question:
bytes_read = ReadLine ( file_handle, line_buffer, maximum_bytes );
the second argument is the buffer to store the characters read, thus it is an array of characters; it needs to be large enough to hold maximum_bytes plus the NULL, so char [ maximum_butes + 1 ]
So, obviously the number of lines in your text tiles can be determined in a loop:
//open the file
lines = 0;
while ( ReadLine () > 0 )
{
lines ++;
}
// close the file
05-10-2012 07:55 AM
Thankx for your support. My problem is solved. it was not a complex issue but as I am new to CVI and also I have no practical Programming expereince, thats why facing such issues. I'll be able to learn more and more with your response and support. GOD BLESS