与正常模式下正确设置的回车符相比,为什么fprintf在文本模式下的行为有所不同? [英] Why does fprintf behave differently in text mode compared to a properly set carriage return in normal mode?

查看:112
本文介绍了与正常模式下正确设置的回车符相比,为什么fprintf在文本模式下的行为有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题更多是好奇心,而不是问题.

The following question is more a curiosity than a problem.

我偶然发现了这个问题,提供了两个看似等效的不同答案.但是事实并非如此,这让我开始思考.

I stumbled over this question, offering two different answers which seem to be equivalent. But they aren't, what made me thinking.

想象一个system调用,它回显两行:

Imagine a system call which echoes two lines:

[~,message] = system( 'echo hello && echo world' );

返回:

hello
world

如果要将这些行写到.txt文件并在记事本中打开它,则常见的方法是:

If one wants to write these lines to a .txt-file and open it in notepad, the common approach would be:

fid = fopen([pwd '\helloworld.txt'],'w');
fprintf(fid, '%s\n', message);
fclose(fid);
winopen('helloworld.txt')

返回

hello world

由于记事本显然不能正确识别换行符\n,因此解决方案是使用'wt'而不是'w'来强制执行文本模式,这应该很慢.返回:

As notepad obviously is not able to recognize the line feed \n properly, the solution is to use 'wt' instead of 'w' to enforce text mode, which is supposed to be slow. return:

hello
world

有关打开权限的文档说:

要以文本模式打开文件,请在权限后面附加字母"t" 参数,例如"rt"或"wt +".

To open files in text mode, attach the letter 't' to the permission argument, such as 'rt' or 'wt+'.

在Windows®系统上,以文本模式:
-读取后跟换行符('\ r \ n')的回车符将从输入中删除回车符.

On Windows® systems, in text mode:
-Read operations that encounter a carriage return followed by a newline character ('\r\n') remove the carriage return from the input.

-Write操作在任何换行符之前插入回车符 在输出中.

-Write operations insert a carriage return before any newline character in the output.

因此,据我了解,它基本上可以做到:

So in my understanding it basically does:

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

但是输出再次是:

hello world

'wt'还有什么? 'w'如何获得相同的行为? 很抱歉,这个问题毫无意义且无关紧要,但是经过几个令人沮丧的时间之后,我很好奇我所缺少的东西.

What else does 'wt'? How could one obtain the same behavior with 'w'? I'm sorry if this question is meaningless and trivial, but after some frustrating hours I'm just curious what I was missing.

推荐答案

据我所知

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

如果您这样做

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

您只需要在邮件的末尾添加一个回车符和换行符,即"world \ n"之后."hello"和"world"之间的换行符将保持不变,而无需回车符.

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.

因此在您的fprintf中,您的消息是"hello\nworld\n\r\n",应该是"hello\r\nworld\r\n"

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

您可以通过读取输出文件(以字节为单位)来进行检查,知道\n将作为10作为uint8,而\r13:

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

这篇关于与正常模式下正确设置的回车符相比,为什么fprintf在文本模式下的行为有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆