Starting a new line while writing to a text file with fprintf()

13 views (last 30 days)
Hello,
I'm trying to write to a text file using fprintf, and using \n to skip to a new line doesn't seems to be working. If I use something like this:
cd('C:\');
fileid=fopen('testbatfile.txt','w')
a1='@echo'
a2='cd=directory'
formatSpec = '%1s\n\r%2s\n\r';
fprintf(fileid,formatSpec,a1,a2)
My result is a text file that appears:
@echocd=directory
Why isn't '\n' working here?
Thanks in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2012
You can open the file as 'wt' instead of 'w' and use \n instead of \n\r or you can open it the way you are now and use \r\n instead of \n\r

More Answers (0)

Community Treasure Hunt

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

Start Hunting!