From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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 a chunk of data on a given position in a txt file

Hello guys,

 

Iam writing a function that will log data in the flavor of the following image. To later on analyze it with DIAdem.File Format.PNG

Iam having trouble inserting the middle part because it overrides part of the buttom section.

I have tried two (2) different ways.

1. I write headings and body and before closing the ref I write the subheadings (middle part) where there is only one space between both "***End of Line***" delimiter expecting to write in between delimiters.

 

File Format2.PNG

2. Leaving the space that it would take for the subheadings between "***End of Line***" delimiters.

File Format1.PNG

 

both variants failed, it doesnt move down the last delimiter, instead it overwrites it.

 

any help in this will be apreaciated.

 

regards,

0 Kudos
Message 1 of 5
(2,896 Views)

Unless the file sizes are going to get really large I would split the entire file to find your 'before' and 'after' point (e.g. into the different sections), use concatenate strings to add your new data to the relevant section and then write the whole lot back out to the file.

 

A slightly more optimised version would find the file position start of the section after the one you're writing to - read all of that into memory, write your data to that file position (until the end of the file) and then append the remainder from memory.

 

The file IO functions don't have an 'insert' option and instead will overwrite the bytes of the file.


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 2 of 5
(2,875 Views)

So in this way, if i want append some message to a txt file, i need to read out the txt, and append, then write to the txt, right ?

If write directly, the whole txt will be overwritten ?

Thanks.

Keep moving
0 Kudos
Message 3 of 5
(1,797 Views)

Not exactly.  You can *append* to the end of text file without reading and rewriting the original contents.  (Open it, seek to the end, write the data you want to append, close it.)

 

This thread addresses the fact that you can't *insert* data between parts of an existing text file.  To accomplish an insert, you *would* need to read and rewrite the existing contents.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 4 of 5
(1,787 Views)

@Brzhou123 wrote:

So in this way, if i want append some message to a txt file, i need to read out the txt, and append, then write to the txt, right ?

If write directly, the whole txt will be overwritten ?


If you are just adding to the end of the text file, you just need to set the file pointer to be the end of the file before you start writing.  You do this by using the Set File Position function (in the File I/O->Advanced palette), setting the "From" to "End" (defaults to "Front"), and leaving the "Offset" input unwired (defaults to 0).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 5
(1,783 Views)