how to modify a file. txt
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello,
I would like some advice from someone to do this job: I have to copy the lines of a text file to an another text file created by me until it comes from a certain string. Arrived at this string, I stop copying and I have to insert at the end of these copied lines other lines of text coming from a third .txt file
Thanks in advance for the advice
댓글 수: 3
MR
2022년 10월 28일
Hi,
does this help you?
% Open file
file = fopen('myFile.txt', 'w');
% Write text into my txt file which I just created
fprintf(file, 'My own Text goes here.');
채택된 답변
Jan
2022년 10월 28일
편집: Jan
2022년 10월 29일
C = readlines('YourFile.txt');
idx = find(contains(C, 'TheWordYouAreSearching'), 1);
writelines(C(1:idx), 'Output.txt'); % [EDITED] Data at 1st, file name at 2nd
D = readlines('The3rdFile.txt');
writelines(D, 'Output.txt', 'WriteMode', 'append'); % [EDITED], see above
댓글 수: 2
Jan
2022년 10월 29일
@Federico Paolucci: If a command fails, look into the documentation:
doc writelines
There you see, that the first argument is the data and the second one the file name. I've swapped them.
The code is fixed now.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Install Products에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!