Filter löschen
Filter löschen

Copy a txt file and change a line by a other

2 Ansichten (letzte 30 Tage)
Lila wagou
Lila wagou am 23 Mär. 2016
Kommentiert: Walter Roberson am 24 Mär. 2016
Dear I have a problem to copy from a txt file to another one, and change a specific line i have found the copy file option
file_A = 'Ok_A.txt';
file_B = 'Ok_B.txt';
copyfile(file_A,file_B)
But i cannot go further

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Mär. 2016
copyfile() is not appropriate for this. You need to fopen() one file for input and the other for output, and then copy the input to the output until you get to the line that is to be changed. Output the changed version of the line rather than the original version of the line. Then continue copying input to output until the end of file. Then fclose() the two files.
There is no way to make a change in the "middle" of a text file -- not unless the new text is exactly the same size as the original text. This is not a restriction imposed by MATLAB: it is a restriction on all file systems that have been implemented by vendors over the last 30-ish years.
  4 Kommentare
Lila wagou
Lila wagou am 24 Mär. 2016
Dear Walter, thank you lot. Please, a small change in the 4 line
fid_out = fopen('OK_B.txt', 'w');
Thank
Walter Roberson
Walter Roberson am 24 Mär. 2016
fid_in = fopen('Ok_A.txt', 'r');
fid_out = fopen('Ok_B.txt', 'w');
You can change the strings to whatever you need.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by