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 a string data to txt. file

Hello all,

I have problem in saving the data to the txt./ note pad file

i can save data to the single line but i couldn't able to save data to multiple lines at every run

ex. if i run once it will save in first line of txt file

1.  33

and if i run the program again it will save the data in same line

1. 33

but i need to save as

1. 33

2. 34

3. 35

at every run it should choose next line if data is present at previous line.

VI attached please check and modified

looking forward for your support

 

Kind regards

vinay

 

 

 

 

 

 

0 Kudos
Message 1 of 11
(4,893 Views)

You need to use the Set File Position function from File IO>Advanced File functions palette. 

Select the "From" input to "End" to append to the file

0 Kudos
Message 2 of 11
(4,889 Views)

Or open the file once, then without closing it, write to it consecutively. Pass the reference in a shift register. Close when done writing. This way the end position will be remembered and strings will be concatenated.

 

It depends on your application what's best: sometimes [open\write\close] is better, sometimes open\[write]\close...

0 Kudos
Message 3 of 11
(4,855 Views)

thanks for replay

I got it by adding read file inside before the write where file will open and data will be written to the next line

regards

vinay

0 Kudos
Message 4 of 11
(4,813 Views)

What does the while loop do? (Nothing)

What does the for loop do? (Almost nothing)

 

You can replace the for loop, build array, array to spreadsheet string and concatenate by just a concatenate. The for loop, build array, array to spreadsheet string don't do anything except add a new line to the DATA1 string.

 

Why use a build array to go from 1D to 2D, and a for loop to go from scalar to 1D? The 2D array is not needed at all, but if you need it, just use two build arrays after each other. Or even better: an Initialize Array.

 

Now this all could be a placeholder for future work of course, but at the moment you have more redundant code then useful code.

 

The read will do the trick (set the pointer to the end). But it's a rough way to do it. At some point someone (future you) will think it doesn't do anything and delete it. At least comment why it's there.

 

With a Set Position, it will be clearer and potentially faster. The read might just really allocate memory and copy the data, you shouldn't depend on a compiler to remove that action...

0 Kudos
Message 5 of 11
(4,806 Views)

wiebe@CARYA wrote:

The read will do the trick (set the pointer to the end). But it's a rough way to do it. At some point someone (future you) will think it doesn't do anything and delete it. At least comment why it's there.

 

With a Set Position, it will be clearer and potentially faster. The read might just really allocate memory and copy the data, you shouldn't depend on a compiler to remove that action...


Completely agree here.  There is a lot that happens to "Read" a file.  Just setting the file position is a lot less work for your application and is more clear what is happening.


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 6 of 11
(4,801 Views)

Hello Crossrulz,

 

VI is working fine as my requirement and I have done changes like removing the for loop inside and also removing unnecessary blocks and also its working fine 

but placing the read option inside the loop it may keep memory like position of file I feel.

please correct me if I am I wrong

 

Thanks

vinay  

 

0 Kudos
Message 7 of 11
(4,774 Views)

@Vinaygowda wrote:

 

but placing the read option inside the loop it may keep memory like position of file I feel.

please correct me if I am I wrong 


Yes, it does that. Indeed, it 'works'. Nothing wrong with that argument.

 

But it does more then you need, it might actually allocate memory, and a file read and data copy. This could become a problem if the file gets large. The read might take more and more time, as the file grows. Definitely not something you should want.

 

The Set Position on the other hand, has the same effect, but without the cost of the read. And it communicates to others (future you) what it does and why. 

 

You only have to do it once, after opening the file:

 

Open
Set Position
For each value
    Write
Close

As reads and writes will also set the position... 

 

And of course the express VI "Write to Measurement File.vi" does this as an option, but I don't really recommend those express VIs, as they tend to need replacement sooner or later.

0 Kudos
Message 8 of 11
(4,765 Views)

      Hello  

 

thanks for replay

the file read and write is small as I need only 10 line i.e ( 1 to 10) / 3 digit number to write to file and after 10 line the file will save and close and the next 10 line is written in new file this is the requirement of project as of now

and its working fine for my application

one more question does it take more processing power to execute or its not a good way to do LabView program

 

have a nice day

regards

Vinay

 

0 Kudos
Message 9 of 11
(4,721 Views)

hi vinaygowda,

opening and closing files is an "expensive" operation.

but since you have the requirement to write to multiple files,

you won't get around that.

 

 

a quick note:

replay == play again

reply == answer


If Tetris has taught me anything, it's errors pile up and accomplishments disappear.
0 Kudos
Message 10 of 11
(4,713 Views)