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

2 visualizaciones (últimos 30 días)
Yash
Yash el 14 de Jul. de 2014
Respondida: Andrew el 14 de Jul. de 2014
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?

Respuestas (1)

Andrew
Andrew el 14 de Jul. de 2014
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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by