write to a text file...

2 visualizaciones (últimos 30 días)
Loran
Loran el 20 de Sept. de 2014
Respondida: Guillaume el 20 de Sept. de 2014
Hello,
I want to read a text file and write it in to a desire line in the other text file?
Any help would be greatly appreciated!
thanks so much.
Loran
For example: I have a big text file and would like to add another text file data under the 'TIME 380' line. How should I search for the "TIME 380' line and add the data underneath..?
…..
TIME 350
TIME 360
TIME 370
TIME 380
++++ data from a text file++++
TIME 390
TIME 400
  1 comentario
Stephen23
Stephen23 el 20 de Sept. de 2014
Editada: Stephen23 el 20 de Sept. de 2014
Double-posting will not encourage people to answer your questions. Please edit your original question , if it was not clear the first time you wrote it.
To solve your problem: learn to use a search engine, and read this:

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 20 de Sept. de 2014
bigfid = fopen(bigfile, 'rt+'); %open the big file in read/write text mode
infid = fopen(otherfile, 'rt'); %open the other file in read text mode
%read lines until you get to the insertion point:
l = fgetl(bigfid); %read first line
while ~strcmp(l, 'TIME 380') %or other comparison functions
l = fgetl(bigfid);
end
insertpos = ftell(bigfid); %memorise insertion point
remainder = fread(bigfid); %read the rest of the file to rewrite after insertion
fseek(bigfid, insertpos, 'bof'); rewind to insertion point
fwrite(bigfid, fread(infid)); %copy content of other file at insertion point
fwrite(bigfid, remainder); %and write back the rest of the big file
fclose(bigfid);
fclose(infid);
Untested, there may be some minor errors, but you get the idea.

Categorías

Más información sobre Desktop 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