LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to find the number of data items in a file written with ArryToFile function?

Solved!
Go to solution

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

If you are young work to Learn, not to earn.
0 Kudos
Message 1 of 7
(3,976 Views)

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.

0 Kudos
Message 2 of 7
(3,975 Views)

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

0 Kudos
Message 3 of 7
(3,971 Views)

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.

If you are young work to Learn, not to earn.
0 Kudos
Message 4 of 7
(3,958 Views)

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!



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 5 of 7
(3,951 Views)
Solution
Accepted by topic author Dreamer14388

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

Message 6 of 7
(3,948 Views)

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 

If you are young work to Learn, not to earn.
0 Kudos
Message 7 of 7
(3,927 Views)