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