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

2 vues (au cours des 30 derniers jours)
Yash
Yash le 14 Juil 2014
Réponse apportée : Andrew le 14 Juil 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?

Réponses (1)

Andrew
Andrew le 14 Juil 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

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by