write to a text file...

조회 수: 2 (최근 30일)
Loran
Loran 2014년 9월 20일
답변: Guillaume 2014년 9월 20일
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
Stephen23
Stephen23 2014년 9월 20일
편집: Stephen23 2014년 9월 20일
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:

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2014년 9월 20일
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.

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by