append the content of a txt to the end of a txt....
24 ビュー (過去 30 日間)
古いコメントを表示
how can i append the content of content.txt to the end of the 33.txt
i think the code should be something like that...
clc
fid1 = fopen('C:\Users\Mr Andrew\Desktop\content.txt', 'r');
fid = fopen('C:\Users\Mr Andrew\Desktop\33.txt', 'a');
for k = 1:5
fprintf(fid,'fid1 \r\n') ;
end
fclose(fid);
fclose(fid1);
thank you
0 件のコメント
採用された回答
Cedric
2013 年 8 月 26 日
編集済み: Cedric
2013 年 8 月 26 日
You can read/write line by line with something like
while ~feof(fid1)
line = fgetl(fid1) ;
fprintf(fid, '%s\r\n', line) ;
end
but if you don't need to process the content, you could go for a more direct way, e.g.
fid = fopen('content.txt', 'a') ;
fwrite(fid, fileread('33.txt')) ;
fclose(fid) ;
Now you could have a more robust approach which reads the content of the first file, determines if it ends with '\r\n', and adds it if not before appending the content of the second file.
その他の回答 (1 件)
dpb
2013 年 8 月 26 日
Which OS? Under Win, I'd just use
system('copy 33.txt+content.txt')
To make a new target file, also specify a destination filename...
system('copy 33.txt+content.txt bigtextfile.txt')
I'm certain the other OS has something similar but not positive about syntax.
Inside Matlab your proposed script doesn't work as written -- you open the one for append and the other for read access as have but then must read the other in explicitly and write out -- which is the easiest way to do that is dependent on the content in the file. I'm not certain what the loop w/ upper limit of 5 is trying to do???
fi = fopen('C:\Users\Mr Andrew\Desktop\content.txt', 'r');
fo = fopen('C:\Users\Mr Andrew\Desktop\33.txt', 'wa');
while ~feof(fi)
l=fgetl(fi); % get line from input
fprintf(fp,'%s\n',l); % write to output
end
fi=fclose(fi);
fo=fclose(fo);
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!