フィルターのクリア

How do you write to a fopen text file without deleting previous text?

60 ビュー (過去 30 日間)
Recap
Recap 2016 年 4 月 2 日
コメント済み: Image Analyst 2021 年 10 月 12 日
I'm writing the results of my program to a text file. But every time I run the program and open the text file using fopen, it overwrites everything that was previously written in that file. What can I do to prevent it overwriting the previous text?
Im opening the file using
fileID = fopen('myfile.txt','r+');
and writing to it
fprintf(fileID,'%d',Number(num));

採用された回答

Image Analyst
Image Analyst 2016 年 4 月 2 日
Open two separate files. Then write the output file. Afterwards, you can delete the input file and rename the output file if you want.
fInput = fopen(inputFileName, 'rt');
fOutput = fopen(outputFileName, 'wt');
% Now read from input file and write to output file
thisLine = fgetl(fInput);
fprintf(fOutput........
% All done. Close both files.
fclose(fInput);
fclose(fOutput);
% Now delete and/or rename any files you want.
  2 件のコメント
VBBV
VBBV 2021 年 10 月 12 日
If I want to replace a specific text string in a file that's filled with both numeric and text data. Will the above solution work ? I want to write to new file with all the data in old file but the specfic text string replaced.
Image Analyst
Image Analyst 2021 年 10 月 12 日
You could do
% Open the file for reading in text mode.
fInput = fopen(inputFileName, 'rt');
% Open the file for writing in text mode.
fOutput = fopen(outputFileName, 'wt');
% Now read from input file and write to output file
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
strPattern = 'This is the line I want to replace'; % Adapt as needed.
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
if strcmpi(textLine, strPattern)
% We found the string we're looking for.
% Replace it with what we want.
textLine = 'This is my new Line.'; % Adapt as needed.
end
fprintf(fOutput, '%s', textLine);
end
% All done. Close both files.
fclose(fInput);
Or you could more compactly use fileread() and strrep().
inputString = fileread(inputFileName);
newString = strrep(inputString, oldPattern, newPattern)
% Open the file for writing in text mode.
fOutput = fopen(outputFileName, 'wt');
fprintf(fOutput, '%s', newString);
fclose(fOutput);
I haven't tested either - they're just off the top of my head.

サインインしてコメントする。

その他の回答 (2 件)

Stephen23
Stephen23 2016 年 4 月 2 日
Simply use the append option a:
fileID = fopen('myfile.txt','a+');
That is it.
  1 件のコメント
Image Analyst
Image Analyst 2016 年 4 月 2 日
He never mentioned append, but I think you might be right. If only he had mentioned that at first....

サインインしてコメントする。


Muhammad Usman Saleem
Muhammad Usman Saleem 2016 年 4 月 2 日
編集済み: Muhammad Usman Saleem 2016 年 4 月 2 日
do not use r+ read reason form documentation here
change this line to
fileID = fopen('myfile.txt','w'); % opening for writing only
r+ for both reading and writing. Which change previous content
  5 件のコメント
Muhammad Usman Saleem
Muhammad Usman Saleem 2016 年 4 月 2 日
編集済み: Muhammad Usman Saleem 2016 年 4 月 2 日
what is your matrix Number? how can it contains string? I am supperise to know matrix with string and numeric?
Recap
Recap 2016 年 4 月 2 日
Number is doesnt not contain string. it contain values[1 2 3 4 5] Letter is a string containing 'D'

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by