How can I copy lines of an external text file to another file

40 ビュー (過去 30 日間)
Hugo
Hugo 2022 年 4 月 26 日
コメント済み: Stephen23 2022 年 4 月 29 日
Hi,
I would like to know how can I open a file, named a.txt (which in attach), and copy all the lines, from the line 5 to the end of file, to another text file, named b.txt
I thank you in advance,
Best regards,

回答 (2 件)

Stephen23
Stephen23 2022 年 4 月 26 日
tmp = readlines('a.txt');
writelines(tmp(5:end),'new.txt')
  2 件のコメント
Hugo
Hugo 2022 年 4 月 29 日
Hi,
I got the following error when trying your solution:
"Unrecognized function or variable 'readlines'."
Stephen23
Stephen23 2022 年 4 月 29 日
Yes, unfortunately READLINES was introduced in R2020b.

サインインしてコメントする。


Voss
Voss 2022 年 4 月 26 日
Here's one way:
fid_in = fopen('a.txt'); % input file
fid_out = fopen('b.txt','w'); % output file
for ii = 1:4 % read (and discard) the first four lines
fgetl(fid_in);
end
data = fread(fid_in); % read the rest of the input file
fclose(fid_in); % close the input file
fprintf(fid_out,'%s',data); % write data to the output file
fclose(fid_out); % close the output file
type b.txt % check the contents of b.txt
56h af4 ghj67 asdad23 asd8 hj768 sdf234 dh568 asd45234 fggh768 rfa423 asdas4234 isdas423 hk89 d456 v234 h671s k78s1

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by