fprintf without empty lines
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everybody,
I have some trouble with fprintf. I want to save a single line of characters in a txt-file. Later on I want to save another single line (chars again) in the same file. But there should not be any empty lines between nonempty lines in the end.
But in the way I coded my function, fprintf exports three empty lines after each line of chars. Even if I use the control characters '\n' or '\r\n' to start a new line.
Does someone know how to fix this?
Here is my code:
filename_user_AWG = [pwd '\resource\txt\user_AWG.txt'];
output = cell2table(AWG_out_str);
writetable(output, filename_helperfile, 'Delimiter', ';', 'WriteVariableNames', false, 'WriteRowNames', false);
fid = fopen(filename_helperfile, 'r');
tempfilecontent = fread(fid,'*char')';
fclose(fid);
fid = fopen(filename_user_AWG, 'at');
fprintf(fid, '%s', tempfilecontent);
fclose(fid);
Greetings, Stefan
0 commentaires
Réponse acceptée
dpb
le 19 Déc 2014
...
fid = fopen(filename_helperfile, 'r');
tempfilecontent = fread(fid,'*char')';
fclose(fid);
fid = fopen(filename_user_AWG, 'at');
fprintf(fid, '%s', tempfilecontent);
Any newline characters in the output file here are simply those echoed from the input content of the input file and that are embedded in the tempfilecontent variable.
4 commentaires
dpb
le 28 Déc 2014
...how you would use strrep in my case
Needs must first determine what the file newline character(s) is/are...but the basic premise is if you read the file as a character array as you did with fread, then
temp=strrep(temp,char(10),''); % remove LF characters
On Windows, \n is a CR/LF pair so need both char(10) and char(13) (0A and 0D hex). Unix uses only the single 0A.
...but it would be nice to have a clean txt-file also.
Not sure what you're saying here; textscan or one of the formatted file input routines is the clean way to read a text file; you got unexpected results by reading a formatted file as an exact character copy including the control characters by the use of fread before...
If you mean that you have a file that does have embedded blank lines and wish to create one that has the same content and line breaks but without any blank lines, then either use the "trick" above except
temp=strrep(temp,[char(10) char(10)],char(10)); % remove doubled LF's
The above needs to be applied repetitively until no additional substitutions are found if there can be more than one repeated blank lines.
Alternatively, write a filter...
while ~feof(fid1)
l=fgetl(fid1);
if length(l)==0, continue, end % skip empty lines
fprintf(fid2,'%s\n',l) % write non-empty to new file
end
fgetl reads a line w/o the trailing linefeed so an empty line is length zero; Matlab handles the \n conundrum internally transparently.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Object Programming dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!