How can I edit and external text ('.txt') file?

4 views (last 30 days)
I have a text file, and I want to edit a value of an especific line.
For example, the 17th line of the text file is:
11XX0001 0 24500. -1. 1.E3
and I want to edit it for
11XX0001 0 357. -1. 1.E3
While the rest of the file shall be the same. Thank you very much, in advance!

Answers (1)

Walter Roberson
Walter Roberson on 23 Sep 2016
Because your replacement is a different size than your input, you will need to create a new output file with the modifications; you will not be able to make the change "in place".
Open the input file, open the output file, loop 16 times using fgets() from input and fwrite() to output. fgets the 17th line and make the change to the string as required. Then fwrite() the new string to the output. Then loop over the rest of the file, fgets() and fwrite(). Finally fclose() both files.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!