Read m file with fgets / fgetl
Show older comments
Hi,
I want to read a m file called 'File1.m' and write in the same time a 'File2.m'.
My code (simplified for example) is :
Fin = fopen('File1.m','r');
Fout = fopen('File2.m','w');
while feof(Fin) == 0
line = fgets(Fin);
if regexp(line,'text')
fprintf(Fout,' modified text ');
else
fprintf(Fout,line);
end
end
It works perfectly EXCEPT that fgets is not reading comments in File1 (comments indicated with '%').
Do you know a way to read them? It is actually important comments.
Thanks, Clément
3 Comments
dpb
on 19 Apr 2016
Don't think your diagnosis of your symptom is correct...there's no reason to think fgets will not read a line from a file whatever the character. Note your output is going to be all on one line as fgets strips the \n; maybe it's a display issue...
Clément P
on 19 Apr 2016
Accepted Answer
More Answers (0)
Categories
Find more on Low-Level File I/O 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!