LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

overwrite a record in an ascii file

Hi

I am looking for a specific variable/value pair in an ascii file, once I found the line that contains that variable, I want to change its value to a new value. Can someone help me to overwrite this line with new line that contains the updated value for the variable.

In case the variable is not found, I want to add a new line at the end of file with this variable/value pair.

Thanks.
0 Kudos
Message 1 of 3
(3,025 Views)
The problem can arise if the new line has different lenght than original one and the values in the files are written one after the other without extra spaces. In this case the simplest thing I can think of is to create a new file with the updated content and then delete the previous one.
Some line in pseudo-code:

destH = OpenFile (NewFile) // Open in ASCII mode
srcH = OpenFile (TheOriginalFile) // Open in ASCII Mode
while (br != -2) {
br = ReadLine (srcH...)
// Scan the string
If the desired variable: Update the string
In any case: WriteLine (destH...)
}
CloseFile (srcH);
CloseFile (destH);
DeleteFile (original file)
RenameFile (new file as original one)


Hope all this is clear enough
Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 3
(3,018 Views)
Depeding on the format of the variable/value relationship you may be able to use the LabWindows/CVI 'Reading/Writing .ini Style Files' functions. You can use this if the file is in the following format.

[SectionName]
variable1 = value1
variable2 = value2
etc...

The function panel that contains these functions is located at
'\toolslib\toolbox\inifile.fp'
0 Kudos
Message 3 of 3
(3,012 Views)