C++ 换行和回车

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7372012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:50:25  来源:igfitidea点击:

Line Feed and Carriage Return

c++

提问by Raven

Recently I was working with some strings, text input, things like that and I realized I got little confused about 2 characters - LF(10) and CR(13). Every time I needed to start new line I used std::endl for c++ string and \n which is LF for c-strings. However I'm now using one library which on return key press sends me not LF but CR key code. I read on wikipedia that usage is as follows:

最近我在处理一些字符串、文本输入之类的事情,我意识到我对 2 个字符 - LF(10) 和 CR(13) 有点困惑。每次我需要开始新行时,我都将 std::endl 用于 c++ 字符串,而 \n 是 c 字符串的 LF。但是,我现在正在使用一个库,该库在按下返回键时向我发送的不是 LF 而是 CR 键代码。我在维基百科上读到用法如下:

CR+LF: Microsoft Windows, DEC TOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC-DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS
LF+CR: Acorn BBC spooled text output.
CR:    Commodore 8-bit machines, Acorn BBC, TRS-80, Apple II family, Mac OS up to version 9 and OS-9
LF:    Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others.
RS:    QNX pre-POSIX implementation.

But I never really noticed need for CR on windows, everything gets printed anyway at right location. CR, again according to wikipedia, was used in times of type writers for returning that writing head to begining of line, and then LF for scrolling one line bellow.

但是我从来没有真正注意到在 Windows 上需要 CR,无论如何,所有内容都会在正确的位置打印。再次根据维基百科,CR 用于打字机时代,用于将书写头返回到行首,然后 LF 用于滚动下面一行。

My question is if it's really necessary these days to use CR and why. What systems could possibly fail to output text correctly if only LF is used? Does printers still require CR and if so, does OS automaticly interpret LF as both new line and return to start of line or CR must be still hardcoded in data that I sent for printing?

我的问题是这些天是否真的有必要使用 CR 以及为什么。如果仅使用 LF,哪些系统可能无法正确输出文本?打印机是否仍然需要 CR,如果是,操作系统是否会自动将 LF 解释为新行并返回到行首,或者 CR 仍然必须在我发送用于打印的数据中进行硬编码?

回答by Matteo Italia

Inside your C and C++ programs all you need (at least, when dealing just with the standard library) is \n, which, when sent to any C/C++ stream opened in text mode(i.e. when you don't specify binto the fopenand ios::binfor C++ streams), is automatically translated to the line terminator of the current platform. That's why on Windows you can just write \nto any stream and it becomes "magically" CRLF into the file/on the console.

里面你的C和C ++程序,所有你需要(至少,与标准库刚交往时)时\n,当被发送到任何C / C ++流在文本模式下打开时(即不指定bfopenios::bin用于C ++流),自动转换为当前平台的行终止符。这就是为什么在 Windows 上你可以只写入\n任何流,它变成“神奇地”CRLF 到文件/控制台上。

The whole binary/text mode thing exist for this purpose: when you are writing a text file it's useful to have this translation (this way inside your strings you can just have \nas line terminator without worrying about the specific platform line terminator), but when you're writing a binary file \nis just a byte like the others and should not be translated, otherwise you get corrupted data.

整个二进制/文本模式都是为此目的而存在的:当您编写文本文件时,进行此转换很有用(这样在您的字符串中,您可以将其\n用作行终止符,而无需担心特定的平台行终止符),但是当您正在编写的二进制文件\n与其他文件一样只是一个字节,不应进行翻译,否则您会得到损坏的数据。

*NIX systems that use just LF (which is \n) don't actually do any translation, but it's still good to specify correctly binary/text mode for portability/clarity purposes.

*仅使用 LF(即\n)的NIX 系统实际上不进行任何翻译,但为了可移植性/清晰性目的,正确指定二进制/文本模式仍然很好。

Using always endlin C++ is a common mistake, \nis enough to get the translation to the platform-specific line terminator.

endl在 C++ 中使用 always是一个常见的错误,\n足以转换为特定于平台的行终止符。

What endldoes more than \nis to flush the stream buffer, which can be useful in some limited circumstances (e.g. outputting something on a console before a long operation), but in general just slows down the IO (on consoles it's usually not noticeable, but on files it is). I usually just use \nand add a std::flushwhen a flush is actually needed.

什么endl做多\n是刷新流缓冲区,可以在某些有限的情况下非常有用(如输出长时间操作之前在控制台上的东西),但一般只是会减慢IO(游戏机上它通常并不明显,但在文件是)。我通常只是在实际需要冲洗时使用\n并添加一个std::flush



This for what concerns the standard library; when dealing with other libraries YMMV and you should check their documentation to see if they follow the standard C convention or they require strings to contain the platform-specific line terminator.

这与标准库有关;在处理其他库 YMMV 时,您应该检查它们的文档以查看它们是否遵循标准的 C 约定,或者它们是否需要字符串来包含特定于平台的行终止符。