write to a text file...
5 views (last 30 days)
Show older comments
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 Comment
Answers (1)
Guillaume
on 20 Sep 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.
0 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!