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

4 visualizzazioni (ultimi 30 giorni)
Matheus Duarte
Matheus Duarte il 23 Set 2016
Risposto: Walter Roberson il 23 Set 2016
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!

Risposte (1)

Walter Roberson
Walter Roberson il 23 Set 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.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by