From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Append to textfile at start (without overwriting existing data)

I want to append text to a existing text file at the start of the file without that the data the file already contains gets overwritten... Does LabVIEW have a VI for this? The VI's in the FILE I/O pallet don't cut it... in the example below my existing data just gets overwritten...

Append.png

File contains "12346789" after appending "test56789"

0 Kudos
Message 1 of 11
(7,047 Views)

Set File Position after you open but before you write.

 

SetFilePos.png

 

Omar
0 Kudos
Message 2 of 11
(7,039 Views)

No Omar, I want to append at the start not the end.

0 Kudos
Message 3 of 11
(7,034 Views)

You then have to read in the file, write the new stuff and then append the old after the new.

 

Omar
0 Kudos
Message 4 of 11
(7,030 Views)

I have thought of that solution as well but it is not acceptable in my case... Ain't there a better solution?

0 Kudos
Message 5 of 11
(7,024 Views)

No. The way files are stored on disc is sequential.  To prepend data to the front you must move every byte in the file to make room for it.  The only way that works in practice is to read the entire file, place the new data where you want it, and then write all the data to the file.

 

One approach which might work for you is to append the new data to the end of the file while you are gathering the data.  When you are finished, read it in, reverse the order of the rows/lines and write it back to the file in the desired final order.

 

Lynn

Message 6 of 11
(7,021 Views)

Thanks for the info. And exactly that is how I do it now. Not very efficient but well...

0 Kudos
Message 7 of 11
(7,015 Views)

Unfortunately, what you have is an operating system limitation, not a LabVIEW limitation. You'd have the same exact problem in any other programming language.  You didn't indicate what is "inefficient" about this. If you are dealing with a very large file and you are concerned about memory then another option is to first write out the prepended data (note the use of the word prepend rather than append, which is to add to the end) to a temporary file, then read in the original file in chunks and append them to this temporary file. Your temporary file will be the original with the prepended data. Here you are substituting file I/O for memory. 

Message 8 of 11
(6,998 Views)

You can also do this in-place in a single file, but will need to buffer the "replaced" data.  The sequence would be something like this:

 

  1. Create a buffer for data at the beginning of the file which will be overwritten.  This can be in memory or in another file, if it is anticipated that the amount will get very large.  This buffer can also be dynamically sized.  It will be used as a static buffer for data overwritten in the file, then a ring buffer when updating the file.
  2. When you write to the file, first read the existing data and put it in the buffer, then overwrite it in the file.
  3. When finished, create another buffer of size 65,000 bytes.
  4. Read the next 65,000 bytes of data from the file and put it in the second buffer.
  5. Write 65,000 bytes from your first buffer into the file.
  6. Copy the 65,000 bytes from the second buffer into the first buffer, overwriting the data you just wrote to the file.  The first buffer is now being used as a ring buffer, so you will need to cache read/write locations.
  7. Repeat 4 and 6 until you have rewritten your entire file.

The 65,000 byte value is the optimum for fast file transfer under Windows (all flavors, all disk format types), but I have not tested it under linux or OSX.  If you need more details, let us know.

Message 9 of 11
(6,979 Views)

Hey Lynn,

I am trying to prepend a string to the beginning of a text file but cannot get the "concatenate" or "build array" VI's to append my old data to the end of my new data. Initially, I am writing text to a file in a while loop for some duration of time. After I stop the while loop, I would like to prepend a string prior to the data recorded in the while loop when I open the data file but I cannot successfully append this "old" data to the end of my string.

 

What am I missing conceptually? I would prefer to write the data immediately rather than wait. All I want is to have a string control to allow me to take notes as the program is running then be able to have these notes show at the beginning of my data.

 

I'd appreciate any help or direction as I am completely stumped; thanks,

 

 Zach

0 Kudos
Message 10 of 11
(6,362 Views)