How to overwrite existing text file without saving as new file

20 ビュー (過去 30 日間)
Michael Boyle
Michael Boyle 2023 年 10 月 13 日
回答済み: Walter Roberson 2023 年 10 月 13 日
I am trying to overwrite the last line in a text file and save it. I don't want to write to a new file as the name needs to be the same before and after. Unfortunately, I can't just append text to the file, because the last line of the file before editing has to change. Thanks!

回答 (3 件)

Walter Roberson
Walter Roberson 2023 年 10 月 13 日
The easiest approach in MATLAB is to read the entire file, change the last line, and write out the result to the original file name.
This has a bit of risk in that if something goes wrong during the writing, then you could end up with a file that is incomplete.
You might think that surely there must be a way to only change the last line. Unfortunately, the way that text files are structured in any operating system you are likely to ever use, the operating system is not keeping track of where the line boundaries are, and the best you can do is use line reading and file information operations to start at the beginning of the file and progress a line at a time figuring out where the lines are, then tell the file system to move you back to the beginning of the last line (based on the information about position that you recorded) and then tell it to write a line from there. Once you have written the new line, if it was at least as long as the old line, you are fine -- but if it was shorter than the previous line so that the overall file would be shorter, you have a problem. Windows and Linux and MacOS all define internal calls to allow you to "truncate" a file at the current position, but MATLAB does not provide any interface to such a function so you are stuck (and need to do something like write blanks to where the file used to end.) This all is a nuisance, so doing operations "in-place" in a file is usually only done for binary files, and for text files you typically end up reading everything, clobbering the old file, then writing all of the updated information.

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 10 月 13 日
If understood correctly, use 'append' option with writematrix():
M = magic(5);
writematrix(M,'MN.txt')
N = M+2;
writematrix(N,'MN.txt','WriteMode','append')
type MN.txt
17,24,1,8,15 23,5,7,14,16 4,6,13,20,22 10,12,19,21,3 11,18,25,2,9 19,26,3,10,17 25,7,9,16,18 6,8,15,22,24 12,14,21,23,5 13,20,27,4,11
% ALt. way is using dlmwrite() which is not recommended though!
M = magic(5);
dlmwrite('MN2.txt',M,'delimiter',' ','roffset',1)
N = M+2;
dlmwrite('MN2.txt',N,'-append','delimiter',' ','roffset',1)
type MN2.txt
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 19 26 3 10 17 25 7 9 16 18 6 8 15 22 24 12 14 21 23 5 13 20 27 4 11

Florian Bidaud
Florian Bidaud 2023 年 10 月 13 日
編集済み: Florian Bidaud 2023 年 10 月 13 日
Hi,
Might not be the fastest to do but it should work:
text_file = 'text.txt';
text_data = readcell(text_file);
text_data{end} = 'your new line replacing the last one';
writecell(text_data,text_file);

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by