Filter löschen
Filter löschen

fprintf without empty lines

9 Ansichten (letzte 30 Tage)
Stefan
Stefan am 19 Dez. 2014
Kommentiert: dpb am 28 Dez. 2014
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

Akzeptierte Antwort

dpb
dpb am 19 Dez. 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 Kommentare
Stefan
Stefan am 28 Dez. 2014
Can you explain how you would use strrep in my case, please?
I now figured out, that when I read the txt-file again using textscan, the empty lines are ignored. So my program works fine without any issues, but it would be nice to have a clean txt-file also.
Thanks, Stefan
dpb
dpb am 28 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by