LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Replace a first line (txt file) with another (write in file)

Solved!
Go to solution

Hi,
how i can replace a first line (txt file tabulaton for example) with another,
example:
Test.txt ( tabulation)

AsZ             Baz             eC             Deaz             Eazd
4             5             4             2             1
12             41             41             1             2

 to

A             B             C             D             E
4             5             4             2             1
12             41             41             1             2

And write that in the file ,
Thank you ,

0 Kudos
Message 1 of 6
(3,619 Views)
Solution
Accepted by topic author Sarah_LAB

You basically need to copy the file.  One way to proceed is as follows:

  1. Open "Original.txt".
  2. Read in one line.
  3. Modify the line as necessary.
  4. Open "Copy.txt".
  5. Write the modified line.
  6. In a For Loop, read one line from Original and write it to Copy.
  7. Close both files.
  8. Delete Original (or if you want to be safe, rename it to Former Original).
  9. Rename Copy to Original.

As far as I know, there is no way to directly write within a file (as there's no way to "shift" the characters to the right of the insertion point as characters are added or deleted).

 

Bob Schor

0 Kudos
Message 2 of 6
(3,608 Views)

There is no other simple method, or a direct labview function?

0 Kudos
Message 3 of 6
(3,607 Views)

Thank you 

0 Kudos
Message 4 of 6
(3,600 Views)

Imaduino,

     Read the last paragraph in my reply -- it explains the difficulty in moving bits around that are stored on media like a disk drive (think about adding or subtracting a few bytes at the beginning of a 1 MB file).  

 

Bob Schor

0 Kudos
Message 5 of 6
(3,595 Views)
Solution
Accepted by topic author Sarah_LAB

Hi,

 

you might overwrite the "first line" - as long as you use the exactly same number of chars as the original line!

(Text files are just an array of bytes…)

When your new "first line" is shorter you need to fill up with spaces. If it's longer then you need to use Bob's copy method…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 6
(3,594 Views)