LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Read in the last line of a file

Can I use the "Read from File " vi to read ionly the last line of the file? If so, how? Thanks in advance for the help

ssmith@bnl.gov
0 Kudos
Message 1 of 11
(9,814 Views)
If you read the file over and over again, and each time the file is different (new lines) then you could store the number of characters read (the file posisition) in a variable and use that to start the next read. Of course, if the data added to the file consisted of more than one line then you would be reading more than one line, so you would have to examine the data from the file to extract the last line. I kinda would tend to read the whole file over and over and deal with the data as a string.

I suppose another option is to make a copy of the file and then diff them, this would give the change.

Both of these ideas might not work well with large files.

Do you have a program that is logging to a file, and you want to update your program from that data?
0 Kudos
Message 2 of 11
(9,801 Views)
Reading the "last line" of a file implies that you are dealing with an humanly readable ASCII text file and there are special characters (/n or /r or a combination of the two) that signify line breaks.

You can only start reading from a certain byte offset using file I/O. Unless all your lines are equal lenght, It is impossible to find the start of the last line without reading every character leading up to it in one way or another.
So in the most general case, you would need to read the entire file and find the last linefeed.

HOWEVER: Typically, lines are not infinitely long, so if you have an upper estimate of line lenght, you can read a slightly longer tail from the end of the file and find the last linefeed there. You can probably modify the VI I posted here for your purpose, just use an appropriate length. 🙂

Message Edited by altenbach on 06-08-2005 10:42 AM

Message 3 of 11
(9,800 Views)
Yes. Remote weather data is being written to a file and I wish to display this data using LAbview. I have already created a display that shows realtime data as it comes to my computer via TCP/IP. I want to use the same diplay to get data from a file. Like check the file once a minute and get the last line from that file. If you want to look at the display I've created to get a feel for what I'm doing look here: www.uao.bnl.gov/labview/FrontPanel.jpg. Thanks.

ssmith
0 Kudos
Message 4 of 11
(9,794 Views)
I would just read the file, delete it, and then copy a blank file to the same name. When you read, there might be more than one line because your processes are not synchronized, so you have to deal with that.
0 Kudos
Message 5 of 11
(9,789 Views)
Just keep track of the last file location that you have read from the file and place it in a shift register, then next time read from there for newly arrived data.
0 Kudos
Message 6 of 11
(9,785 Views)
I like that idea, but being a noob at this I am not sure how to do that. Could you get me started with some ideas?? Thanks for the help.

ssmith
0 Kudos
Message 7 of 11
(9,779 Views)
I made a quick example (LabVIEW 7.1) that does the writing and reading in two different loops.

Writing a new line to the file occurs every 2 seconds. The read loop checks every 200 ms if the file has grown, and if so, reads the new portion. Let me know if this makes sense. There are a few ways to do this. 😉

I'm sure you can adapt it to your problem.
Message 8 of 11
(9,768 Views)
Altenbach--
I'm using LV7.0, the vi u created won't open.

ssmith
0 Kudos
Message 9 of 11
(9,763 Views)
Sorry about that. Heres the 7.0 version of same. 🙂
0 Kudos
Message 10 of 11
(9,762 Views)