How do I ignoring '\n' at the end of the text file that was created?

1 次查看(过去 30 天)
Hello I created a text file called phonenumbers using the strings that gave true for the condition that I set. I used
fprintf(output, '%s\n', line);
to print it on the output file. The only problem is when I compare the file I created with the solution file, I get one extra new line at the end of the file. I am only suppose to have 11 lines but I get 12 lines. How do I fix it. I also have a screenshot below.
Thank you.

采纳的回答

Elias Gule
Elias Gule 2015-2-24
You are appending that extra line. What you can do is change:
fprintf(output,'%s\n',line)
to
fprintf(output,'\n%s',line)
after adding the first line using
fprintf(output,'%s',line)
Example Code:
lines = {'firstPhoneNumber','secondPhoneNumber','thirdPhoneNumber'};
for k = 1 : length(lines)
line = lines{k};
if(k==1)
fprintf(output,'%s',line);
else
fprintf(output,'\n%s',line);
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by