필터 지우기
필터 지우기

append the content of a txt to the end of a txt....

조회 수: 24 (최근 30일)
Andrew
Andrew 2013년 8월 26일
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

채택된 답변

Cedric
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
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 CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by