How can I begin overwriting a txt file from a specific line?

조회 수: 44 (최근 30일)
I'm new to Matlab and I'm not experienced in programming.
The matter is that I have to produce a single output.txt file where I have to print information from 2 different scripts without deleting previous results. In the script1 I used commands like fid, fopen, output.txt 'w' fprintf.. In the second one I would do the same if only I didn't lose the output written before in doing so.
Thanks in advance.

채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 9일
편집: Walter Roberson 2020년 4월 10일
You can fopen the file with 'r+' permission.
You cannot go directly to the the desired line. You can though fgetl or fgets to read in the lines before that point.
Once you have read in the lines before the point that you want to start writing, you need to use fseek() to seek 0 bytes relative to the current point. That sounds like a waste of time, I know, but it tells matlab to get ready to switch to writing.
Then you can start writing the new content.
Warning: any bytes that you do not overwrite will be left the file, and there is no way to discard the rest of a file, and no way to move the rest of the file further left or right, so if you do not replace with exactly the same number of bytes, or else you replace everything to and possibly past the end of file, you are likely to end up with content that you do not want.
Because replacement in the middle of a text file must be for exactly the same number of bytes (not characters), it is almost always a better choice to Don't Do That!
That is, in most cases with text files you should instead create a new file containing the content you want (possibly reading most of it from the old file) instead of changing the inside of an existing text file.
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 4월 9일
If you have the initial part stored in a file, and that does not contain anything that needs to be rewritten, then you can use fileread() to read in the text, then fopen() with 'w', and fwrite() the stuff you fileread(), and then fprintf() the new content. If you have a fixed trailer than you could fileread() that as well and fwrite() it after the variable stuff.
But if the content is short enough and constant enough to make it practical to hard-code, then that can be easier.
Michele Pinchiarul
Michele Pinchiarul 2020년 4월 10일
Oh, maybe this could be easier to implement, thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 4월 9일
편집: Ameer Hamza 2020년 4월 9일
Use append flag
f = fopen('output.txt', 'a');
with append flag, MATLAB start adding data at the end of the opened file without overwriting the current data.
  댓글 수: 2
Michele Pinchiarul
Michele Pinchiarul 2020년 4월 9일
I thought at this possibility, but if I execute the 2nd script 3 times, the same output will append 3 times...
Walter Roberson
Walter Roberson 2020년 4월 10일
Note that the 'a' flag never permits you to write into the middle of a file, and so does not permit you to overwrite starting from a certain point in the file as requested by the user.
In order to be able to write into the middle of an existing file, you should open the file with 'r+' flag.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by