NI Home > Community > NI Discussion Forums

LabVIEW

Showing results for 
Search instead for 
Do you mean 
Reply
Member WG-
Member
WG-
Posts: 187
0 Kudos

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"

Active Participant
Omar_II
Posts: 501
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

[ Edited ]

Set File Position after you open but before you write.

 

SetFilePos.png

 



Omar
Member WG-
Member
WG-
Posts: 187
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

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

Active Participant
Omar_II
Posts: 501
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

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

 

Omar
Member WG-
Member
WG-
Posts: 187
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

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

Knight of NI
johnsold
Posts: 10,282

Re: Append to textfile at start (without overwriting existing data)

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

Member WG-
Member
WG-
Posts: 187
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

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

Knight of NI
Posts: 18,064
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

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. 

Trusted Enthusiast
DFGray
Posts: 2,818
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

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.

Using LabVIEW 2011 - 2013 on Windows 7 64-bit
Member
Zac58
Posts: 8
0 Kudos

Re: Append to textfile at start (without overwriting existing data)

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