write to a text file...
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!