retrieving second to last line in a file

bigalt

Golden Member
Oct 12, 2000
1,525
0
0
Due to awful preparation, we're now stuck trying to juryrig a solution of getting some numbers from labview into a c program.

So right now labview writes 6 numbers of interest, line after line into a file. Hopefully we can do this more often than the c program needs some numbers.

Can a a file be modified by someone else while it is open for read access by a c program? If not then the rest is moot and I might be off the hook.

If I can, my current and awful plan would be:

1) each time I need a set of numbers, open up the file,
2) fast forward to the end
3) wind back some (use an fseek() from SEEK_END?).
3a) If i can't wind back by line, maybe I could go back a certain number of characters, set some kind of marker (a number that wouldn't appear naturally) at the beginning of each line, and read numbers until I find one and then
4) grab the six numbers of interest

are there useful commands that would be useful that I'm just not seeing in the reference libraries?

Thanks so much to the people that helped me out last time, and to anyone who made it through this mess of a request!

edit: the reason i'd want to read the second to last line is in case labview hasn't finished writing the last line. i'm really not sure which one will be relatively faster.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You can open a file for shared, random access reads/writes.

Can you make labview use fixed-width data (padding with spaces as needed) so each line length stays the same? That would make the seeks easy.

What's going to happen eventually though is you'll try to read the file while labview is in the _middle_ of writing.

Plan B:
Can you have labview change the filename for each new write?

When data ready:
1. open file "data< counter >"
2. write 6 numbers, close
3. open file "last-good" <-- truncating / replacing not appending
3. write < counter >
4. counter += 1

The c program then reads "last-good" first to get the index to the most recent file. You could have the counter "roll over" every 100 files and start over.
 

bigalt

Golden Member
Oct 12, 2000
1,525
0
0
Thanks a bunch, I'm going to push those around and see if I can beat the labview program into shape. Right now it's giving me tab delimiters which I'm not sure how to deal with, but hopefully I can make it do something more reasonable.

 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
If you're on UNIX, you can run a shell command to redirect tail to another file and then use that. If your frequency is high enough, using the first item there might be just as good and that'd probably be easier. If not, stepping through that file might be faster on large files, but I don't know.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: bigalt
Thanks a bunch, I'm going to push those around and see if I can beat the labview program into shape. Right now it's giving me tab delimiters which I'm not sure how to deal with, but hopefully I can make it do something more reasonable.

Tab delimiter will not assist in creating fixed length records.
For fixed length, all fields must consistently be the same size.

You problem seeems (as you realize) to be within the LabView handler.

If LabView opens a file as exclusive, then the OS will prevent some-one else from reading it.
Otherwise it will be shared read.

As Dave stated, you will need to ensure that LabView is not updating the file when you do the SEEK_END. After that point, you have a reference that is stable.