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

38 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 9 Apr 2020
Edited: Walter Roberson on 10 Apr 2020
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 Comments
Walter Roberson
Walter Roberson on 9 Apr 2020
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.

Sign in to comment.

More Answers (1)

Ameer Hamza
Ameer Hamza on 9 Apr 2020
Edited: Ameer Hamza on 9 Apr 2020
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 Comments
Michele Pinchiarul
Michele Pinchiarul on 9 Apr 2020
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 on 10 Apr 2020
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.

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!