Borrar filtros
Borrar filtros

Copyfile without overwriting

14 visualizaciones (últimos 30 días)
Diana Acreala
Diana Acreala el 28 de Feb. de 2012
Hello!
How can I copy the content of a *.m file in another. I used copyfile('1.m','2.m') but with this command the content of 2.m will be overwriten by the content of 1.m.
Ex: 1.m content: test1; 2.m content: test 2. I am expecting to get after using copyfile the content of 2.m like: test 2, test1.
I also used the next lines but it is not looking ok.. % fid = fopen('first.m'); % F= fread(fid, '*char')'; % fclose(fid);
Thanks!

Respuesta aceptada

Honglei Chen
Honglei Chen el 28 de Feb. de 2012
I don't think you can do that with copyfile. You can do it with fopen, but you need to open it with option 'a', and then append the content you want.
doc fopen
Here is an example
fid1 = fopen('1.m','r');
fid2 = fopen('2.m','a');
while 1
tline = fgetl(fid1);
if ~ischar(tline), break, end
fprintf(fid2,'%s\n',tline);
end
fclose(fid1);
fclose(fid2);

Más respuestas (1)

Sean de Wolski
Sean de Wolski el 28 de Feb. de 2012
You could use fwrite, or fprintf to write to the end of the file, fgetl would also be your friend.
  6 comentarios
Honglei Chen
Honglei Chen el 28 de Feb. de 2012
I added an example in my answer above
Diana Acreala
Diana Acreala el 28 de Feb. de 2012
Great! I adapted with your example and it's working! Thanks a lot!

Iniciar sesión para comentar.

Categorías

Más información sobre Language Support en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by