LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to replace text in a file

Solved!
Go to solution

Hi everyone,

 

I have a txt file and It has some data's. I want to specifically replace a character in the text file. Data's will be like this,

 

TT=15
S1ST=21601
S1EN=50400
S2ST=50401
S2EN=72000
S3ST=72001
S3EN=21600
GNST=00000
GNEN=00000
S1B1S=22800
S1B1E=23400
S1B2S=28800
S1B2E=30600
S1B3S=36000
S1B3E=36600
S1B4S=45000
S1B4E=46800

 

In the 2nd line i.e. S1ST=21601, I want to replace 36000 instead of 21601.

After replacing the specific character, data's should overwrite in the file or appending the file will also ok for me.

 

 

Give me a suggestion. I am using LV 8.2. So plz post your code in 8.2 ver

Thanks in advance.

 

 

 

Regards,

Vijay

0 Kudos
Message 1 of 13
(8,391 Views)

Check out for file handling function. I just showed one example in attached snippet.

With Regards

Miraz
Kudos is better option to thank somebody on this forum
------------------------------------------------------------------------------
0 Kudos
Message 2 of 13
(8,378 Views)

Hi Vijay,

 

open the file, set the fileposition at the byte of "2" and write a "36000". Close the file. Done.

 

Note: this will only work on text files AND when you replace the same number of chars…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 13
(8,377 Views)

Thanks for the reply GerdW.

 

Solution was almost there. Problem is, TT=15 is not a fixed one. It may vary at different intervals (say, TT=5, TT=22 or TT=450). 

In this case I cant set constant value (byte=2) for offset. Is there any other way ?

0 Kudos
Message 4 of 13
(8,356 Views)

Thanks for ur reply mIraz,

 

I know to read and write string into the file but my question is totally different. I just want to know, how to replace a specific character say "36000" instead of "21601" in the second line "S1ST=21601". 

0 Kudos
Message 5 of 13
(8,347 Views)

Sorry for this reply. Some problem has occured

0 Kudos
Message 6 of 13
(8,346 Views)

When you stated the problem, you said "I want to specifically replace a character in the text file."  You then gave an example where you replaced multiple characters in the file.

 

A text editor that I use a lot has a "binary" mode, where it strictly replaces characters -- you cannot insert or delete characters.  GerdW's solution treats this version of the problem -- it does have a "missing step" that I think he expected you would supply, namely that you need to position the text file at the start of the first character to be replaced, but then you don't want us to do all your work for you, do you?  Are you planning to give GerdW "credit" for this work when you hand it in?

 

On the other hand, if you do not mean "strict character-for-character replacement, no insertion or deletion", you are going to need to do a bit more work.  You still need to find the right place for the insertion of the replacement text, but will also need to read and re-write the text that follows the replacement.  I'm sure you can figure this out for yourself.

 

BS

0 Kudos
Message 7 of 13
(8,334 Views)

Read the entire file as a string.  Then use Search And Replace String to overwrite your values.  Then save the entire file as a string.


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
Message 8 of 13
(8,327 Views)
Solution
Accepted by topic author Vij@y

Check this out.

 

Edit:- attaching 8.2 version

With Regards

Miraz
Kudos is better option to thank somebody on this forum
------------------------------------------------------------------------------
Download All
Message 9 of 13
(8,320 Views)

Hi crozzrulz,

 

This is what actually I tried before. Problem is,the value appending in the text file. This solution is not replacing the values in the text file. Also I wont use more than one text file here.

0 Kudos
Message 10 of 13
(8,280 Views)