TXT file I/O


Recommended Posts


I am using DaqFactory Express for a few laboratory applications using a few Labjacks.

I have one external device that is only able to write data to a computer/network drive/location.

What I would like to do is: open this file every XX seconds (from 2 to 5 seconds) and read the last line. Get the numbers into my application.

Is it possible to do that with Daqfactory express. I am guessing it is not. So if not, is this something that can be done with DaqFactory?


Link to comment
Share on other sites

Thank you.

I was able to open and read data out of a simple text file sample.

Now, I generated the actual Text file from my device. This is the attached file.

As you can see there is a lot of data, but out of all it, I only need to read 4 parameters in the last line of data: See the red frame: those 4 numbers are all I need to pull.

Every time I will read this file, there will be more lines as the device keeps adding to it and the only data that I will need to pull is the set of 4 numbers on the LAST data line.

I have been looking at all functions that are listed in the DAQ-Factory Express help: I have found functions to move through text files but at this point I am not sure if they are powerful enough to reliably pull them.

I would like to get your opinion about that. Do you think I can code (using DAQ factory express) a function that will be able to do that?

Here is what I thought:

- use: File.SeekToEnd(..) to go to the end of the file.

- use: ??? (Something that I hope you could help me with) to move back up "2 lines"

- use: File.ReadDelim(..) to read the parameters I need from this line.

I can't really use File.SeekRelative() to move back up by 2 lines because it requires a precise number of bytes which in my case is variable depending on: both this last line of data AND the very last line which contains a date/time hence a different number of bytes every time.

Last thing I thought out would be to read the entire file and store it into a string variable. From there maybe there are more functions that I could use to seek the right line?

If you have any idea let me know.



Link to comment
Share on other sites

DAQFactory wraps all the standard windows file functions. Random access in files without fixed length fields is an age old problem in programming in general. There are a many solutions. Depending on your file size and structure I'd either read through the file line by line (opening the file as text so read() will read a whole line) then just save the last line read, or use seektoend() then backup some amount that is longer than any possible line length and move forward from there using the same technique.

If you are rereading the same file, you could always store the location of the last line read, jump to that point next time and read the next line.

Link to comment
Share on other sites

Thanks for your answer. I considered as many possibilities as I could think off using the File.functions but the only thing I can think of now would be to save into a variable each line: overwriting the variable each line IF the next one isn't blank.

IF the next one is blank THEN it means I reached the bottom of the file.

Now I have to find a way to program this correctly. Then I will have to pull numbers out of the variable which contain the right line.

Are there functions other than the FILE type to manipulate strings? (like seeking a character, etc?)

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.