LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
NutmegPickles

Prepend to a text file

Status: New

When logging errors to a text file, I would like to see the latest error that occurred rather then the first error to ever happen and have to scroll to the bottom of the file and view the latest error (I know first world problems).  If there was a way to place the cursor at the start of the first line on a text file and enter the data shifting all the original data over/down, that would be helpful.  Currently when the cursor is placed at the start of the file it deletes the data as it is overwritten, if 25 characters is being written, then the first 25 characters of the text file is overwritten.

 

It is possible to read the entire file and concatenate the new data at string 0 with the old data at string 1, but if the file gets large (we don't clean our files / want to remove error data) this can become cumbersome and CPU consuming.

 

https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019RNsSAM&l=en-US - Dated 7 July 2022, states that prepend can not happen the way I would like.

4 Comments
wiebe@CARYA
Knight of NI

The action time will grow linearly with the file size, regardless who performs the action.

 

There's just no way around it. A function that did this (in any language) would still need to open the file, modify the string and write it (either all at once or in parts), with all the problems you described (high CPU, memory and disk bandwidth).

 

Although there are some use cases where it would be convenient (very small files), in those use cases reading the file, modifying it and writing it is a simple enough solution.

 

It's not really a great idea to prepend to files. Databases where invented to solve this problem for users...

fefepeto_kb
Member

While working at NI, I created a TDMS logging library, I don't have access to it anymore, but that basically allowed to add one element of the data to all channels during a write operation (parsed it from a cluster).

I think a similar approach with the TDMS file format could serve your needs, since that would be easy to sort, filtered and even opened in MS Excel.

I admit that the file format was not created for this purpose, but that shouldn't mean you can't use it like that.

wiebe@CARYA
Knight of NI

Wouldn't it be easier to simply append the errors and than reverse in Excel?

fefepeto_kb
Member

That's where I was going to. My solution did not prepend the file either. But after the file was saved (used it during a 3 hours long test run) it could be sorted, filtered and managed in Excel, just as usual.