LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to set the file position with a offset in lines and not in bytes?

I have to read just the last few lines from a very big file. The lengh of the lines is not constant, so I cannot use the VI SetFilePosition (a negative Offset in Bytes).
What can I do?
0 Kudos
Message 1 of 4
(3,592 Views)
hi there
 
there are dozens of possible solutions. my suggestion is to read a increasing amount of bytes from the end of file until the number of lines contained in the string is higher than the desired number (see attachment). you can optimize this by reading only the new data and keep the data already read in a shift register and then call "concatenate strings" function, but this depends on the soze of your file and the number of bytes/lines you want to read. 
Best regards
chris

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 2 of 4
(3,584 Views)
I'm afraid you'll have to build an utility by yourself !
However, that should not be too complicated : assume a typical line length (overstimate it if required), then calculate the number of bytes and read the corresponding file chunck using the calculated negative offset.
Then count the number of EOL chars. If you got the right number of lines, parse the lines, otherwise read another file chunck.
 
The attached vi does a part of the job.
Chilly Charly    (aka CC)
0 Kudos
Message 3 of 4
(3,580 Views)

Just for fun here is another method to add to the mix.

Here I create a batch file that is a basic simulation the unix tail command. The batch file is them executed using the System Exec.vi and the results are parsed to get the last n lines. Its not quick e.g. 5Mb file with ~78000 lines takes about 13 secs to process on my machine. As I said just for fun.

Example attached in 7.1.

David

Edit: for grammar

Message Edited by David Crawford on 07-17-2006 04:04 PM

0 Kudos
Message 4 of 4
(3,562 Views)