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

1 view (last 30 days)
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.

Accepted Answer

Elias Gule
Elias Gule on 24 Feb 2015
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

More Answers (0)

Categories

Find more on Environment and Settings 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!