Open a non empty file, go to a specific row and column and edit(overwrite)

2 次查看(过去 30 天)
Hello, I want to open a file (from command line), say 'cylds.bc', then navigate to a specific line and edit that line. These are the contents of the file:
# BC_SLIPW
PATCH 1 1
NAME SolidWall
#
say i wanted to go to second line and change the number 1 1 to 2 1 . How can I do that?

回答(1 个)

Andrew
Andrew 2014-7-14
you could do something with textscan and strncmp
for instance:
fid = fopen('cylds.bc','r+'); %open file for reading and writing
data=textscan(fid,'%s') %read file as string
data=data{1}; %for some reason maybe someone can explain you need to do this to get to the data
data(strncmp('PATCH',data,1)) = sprintf('%5s %5i %i','PATCH',2,1);
fprintf(fid,'%s',data); %write the data to the file
fclose(fid); %close file
Please note that I have not tested this so there may be a typo

标签

Community Treasure Hunt

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

Start Hunting!

Translated by