11-20-2014 05:35 AM
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
Solved! Go to Solution.
11-20-2014 06:01 AM
Check out for file handling function. I just showed one example in attached snippet.
11-20-2014 06:02 AM - edited 11-20-2014 06:03 AM
11-20-2014 06:24 AM
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 ?
11-20-2014 06:30 AM
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".
11-20-2014 06:30 AM - edited 11-20-2014 06:31 AM
Sorry for this reply. Some problem has occured
11-20-2014 06:38 AM
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
11-20-2014 06:50 AM
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.
11-20-2014 07:01 AM - edited 11-20-2014 07:04 AM
Check this out.
Edit:- attaching 8.2 version
11-20-2014 11:06 AM
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.