how to overwrite a certain value in input file(inp)
Show older comments
I would like to replace the value of roughness of a certain pipe in inp. file. I tried the following but it didn't work.
replace=[PIPES];ID1_roughness;
myformat='%5d \n';
newData=[160];
fid=fopen('Y:\MOD1.inp','r+');
for k=1:(replace-1);
fget1(fid);
end
second question: the command replaceLine how do I combine line and row? for instance the 126th Line and the third row.
Thanks in advance. Wei
Answers (1)
dpb
on 5 Jul 2015
0 votes
The short answer is "you can't" -- sequential files are, well, "sequential"; whatever you write other than appending to the end of an existing file truncates the file at that point and rewrites from that point on.
To do what you want, simply read the file in its entirety; make the modification in the location in memory that you wish and write it back out.
Alternatively, you can use the " fgetl in a loop" strategy but you'll have to have a subsequent second file that copies the input from the first to the second going along with it.
I can find no reference to any such function as replaceLine using TMW doc search.
2 Comments
Walter Roberson
on 6 Jul 2015
No, if you write when you are not positioned at the end of a file (and it is not set up to automatically convert the write to an append) then you overwrite whatever is there but the file does not get any longer or shorter. If you overwrite with exactly the same length of data then it works. But you cannot replace by line, only byte-for-byte.
dpb
on 6 Jul 2015
Strictly speaking, yes, Walter, but in general practice one who has to ask the question will end up closing the file and the OS will write the EOF...
Categories
Find more on Weather and Atmospheric Science 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!