Writing to a specific spot in a text file in Matlab -


i have properties file config.properties program using , has in format:

#comment property1=value1  #comment property2=value2  #comment property3=value3 

my program in matlab needs modify value2 without changing rest of file. hoping there way search property2 , overwrite number after "=" can't find how write preexisting file in specific location when there no delimiters in file.

my question is: how change value2 without changing rest of file?

note: matlab code accessing , changing value2 multiple times while running time position not known first time. file must closed after each change allow access other program.

this clunky solution works.

fid = fopen('test.txt','r');  prop_to_change = 'property2'; newvalue = 'new value';  % find pv pair change while ~feof(fid)     tline = fgetl(fid);     temp = strsplit(tline,'='); % split pv pair 2 cells     if strcmp(temp{1},prop_to_change)         newline = strrep(tline,temp{2},newvalue);         frewind(fid); % go start of file         fulltext = fread(fid,'*char')'; % grab entire file prior discarding         fclose(fid);         fid = fopen('test.txt','w'); % loading write flag discards file contents         fulltext = strrep(fulltext,tline,newline); % replace pv pair         fprintf(fid,'%s',fulltext); % write modified text file         fclose(fid);         break     end end 

test.txt reads:

#comment property1=value1  #comment property2=new value  #comment property3=value3 

reading individual lines , reading in whole thing seems lot of unnecessary overhead. chose way because couldn't think of simpler way pull pv pair without explicitly knowing existing value. can done better using regular expressions have little understanding on how work.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -