Why does fprintf behave differently in text mode compared to a properly set carriage return in normal mode?

断了今生、忘了曾经 提交于 2019-12-06 02:15:41

In my understanding it does

fprintf(fid, '%s', strrep(message, sprintf('\n'), sprintf('\r\n'))

If you do

fprintf(fid, '%s\r\n', message)

you're only adding one carriage return and a newline at the very end of your message, which is after "world\n".The newline character between "hello" and "world" remains without carriage return.

So in your fprintf your message is "hello\nworld\n\r\n", where it should be "hello\r\nworld\r\n"

You can check this by reading the output file in bytes, knowing that \n will be a 10 as uint8 and \r a 13:

>> fid = fopen('test.txt','wt');
>> fprintf(fid, 'hello\nworld\n');
>> fclose(fid);
>> fid = fopen('test.txt','r');
>> bytes = fread(fid, Inf, 'uint8')'

bytes =

   104   101   108   108   111    13    10   119   111   114   108   100    13    10
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!