write to a text file...

3 ビュー (過去 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 Center および File ExchangeDesktop についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by