LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Quick question: How to save strings to a file?

Solved!
Go to solution

Hello,

 

I want to save strings to a .txt file. I can't use wirte to measurement file as string doesn't have timestamp.

I tried to use "wirte to text file", but it seems that it only saves the last value. If I build an array of strings using build array and shift register, everytime I stop and run the vi again, the string array will not be cleared, all new data will be appended to old data. 

 

I'm wondering that is there any solutions? The string format is 111,222   333,444.

 

Thank you so much and have a good thanksgiving!

0 Kudos
Message 1 of 7
(3,592 Views)

If you are using the low level primitive, try to use the "Set File Position" function before using the "Write to File" primitive.


 

 

If I build an array of strings using build array and shift register, everytime I stop and run the vi again, the string array will not be cleared, all new data will be appended to old data. 

Are you initializing the Shift register?

-----

The best solution is the one you find it by yourself
0 Kudos
Message 2 of 7
(3,575 Views)

You don't show us any code (so we can't tell what you've tried, nor what version of LabVIEW you are using), but if you look at the Help for the function called (in my Version) "Write to Text File", and read the Detailed Help, you should learn (a) how to be sure you are appending to the end of a file that already has data, and (b) how to do "incremental" writes, that is, write a line as you get it (without having to accumulate all of the lines in an array).  Note that I'm assuming you are generating the data a line at a time -- you can easily "have your cake and eat it, too", that is, accumulate an array of "all the lines" yet still write only the latest line (just do the Write before doing the "append to array").

 

Bob Schor

0 Kudos
Message 3 of 7
(3,551 Views)

Hi Bob,

 

Sorry I forgot to attach my code.

I just want to read data from serial port, and save strings to a text file constantly.

 

0 Kudos
Message 4 of 7
(3,537 Views)

Thank you P@Anand,

 

Could you please take a look on my code that attached previously?

Write to text file function only saves the latest value.

0 Kudos
Message 5 of 7
(3,536 Views)
Solution
Accepted by topic author Big_man

The snippet below shows the logic of the code you posted, which I called "Write One Line".  If you followed my suggestion to look at the Help for Write to Text File, you would have seen the following:  "If you wire a path, the function opens or creates the file before writing to it and replaces any previous file contents".Write One Line.png

This is exactly what you are seeing, so the function is behaving "as advertised".  However, it goes on to say "If you wire a file refnum to the file input, writing begins at the current file position".  This is the behavior that you want.  So try this model instead (which I call "Write Many Lines":

Write Many Lines.png

You open the file once, before the While loop where you generate "many lines" and write each one, one at a time, to the Text File, closing the file when you exit the loop.  Give that a try (and next time, try to figure things out yourself by carefully reading the Help documents, looking at the Examples suggested at the end of the Help document, etc.).

 

Bob Schor

"

Message 6 of 7
(3,523 Views)

Use File Operations properly Open/(Read/Write)/Close. while opening the file make sure you are providing correct option for operation.

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 7 of 7
(3,517 Views)