Filter löschen
Filter löschen

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

24 Ansichten (letzte 30 Tage)
Andrew
Andrew am 26 Aug. 2013
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

Akzeptierte Antwort

Cedric
Cedric am 26 Aug. 2013
Bearbeitet: Cedric am 26 Aug. 2013
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.

Weitere Antworten (1)

dpb
dpb am 26 Aug. 2013
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);

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by