fprintf end of line character

조회 수: 10 (최근 30일)
Ganesh
Ganesh 2011년 5월 24일
When I use fprintf to print multiple lines of text onto a txt file, it introduces a small rectangular box at the end of each line when ever I use '\n'. How would I print without getting that box at the end of each line of text in txt file?
Thanks, Ganesh
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 5월 24일
So, what is the problem? I thought it was due to the text editor that you are using. Why does '\r\n' solve the problem?

댓글을 달려면 로그인하십시오.

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 5월 24일
fid = fopen('test.txt','w');
fprintf(fid,'%.0f\t%.0f\r\n', [1:5; 6:10]);
fid = fclose(fid);

추가 답변 (4개)

Jan
Jan 2011년 5월 24일
CHAR(10), which is the same as '\n', is a valid line break. Only the dull Windows Editor has problems with the interpretation, but this is a problem of a single outdated program. Some other editors, which can handle the line breaks correctly: The Matlab editor at least since Matlab 5.3, WordPad, Notepad++, Emacs, BBEdit, ...
Using the text mode to write files (fopen(FileName, 'wt')) has some side-effects, which may be very surprising for less experienced programmers.

Fangjun Jiang
Fangjun Jiang 2011년 5월 24일
What text editor are you using to view your text file? You need your text to appear in separated lines, right? Otherwise, you could just not to include the '\n'.
Try '\r' instead.

Walter Roberson
Walter Roberson 2011년 5월 24일
You should be opening your file with 'wt' instead of 'w' when you want to write text files.

Ganesh
Ganesh 2011년 5월 24일
When opening the text file, I am using 'a+' mode for appending at the end. Also, while writing on to the text file, I am using '\r\n' instead of '\n' and it works great.. don't know why but it works! Thanks everyone.
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 5월 24일
You should use 'at+' and then \n would work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by