How can I edit and external text ('.txt') file?
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Walter Roberson
2016-9-23
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!