换行和回车 [英] Line Feed and Carriage Return

查看:96
本文介绍了换行和回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在使用一些字符串,文本输入,这样的事情,我意识到我有点困惑约2个字符 - LF(10)和CR(13)。每次我需要开始新行我使用std :: endl的c ++字符串和\\\
这是LF的c字符串。然而,我现在使用一个库,在返回键按下发送我不LF,但CR键代码。我在wikipedia上阅读使用如下:

  CR + LF:Microsoft Windows,DEC TOPS-10,RT-11大多数其他早期的非Unix和非IBM OSes,CP / M,MP / M,DOS(MS-DOS,PC-DOS等),Atari TOS,OS / 2,Symbian OS,Palm OS 
LF + CR:Acorn BBC假脱机文本输出。
CR:Commodore 8位机器,Acorn BBC,TRS-80,Apple II系列,Mac OS最高版本9和OS-9
LF:Multics,Unix和Unix系统(GNU / Linux,AIX,Xenix,Mac OS X,FreeBSD等),BeOS,Amiga,RISC OS等。
RS:QNX pre-POSIX实现。

但我从来没有真正注意到窗口上需要CR,一切都打印在正确的位置。 CR,再次根据维基百科,在类型写作者的时候用于将写头返回行的开始,然后LF用于滚动一行下行。



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

在你的C和C ++程序中所有你需要的(至少,当处理标准库时) \\\
,当发送到以文本模式打开的任何C / C ++流时(即,当您不将 b $ c> fopen 和 ios :: bin 用于C ++流)将自动转换为当前平台的行终止符。这就是为什么在Windows上你可以写 \\\
到任何流,它变成神奇地CRLF到控制台上的文件/



为了这个目的,整个二进制/文本模式的事情存在:当你写一个文本文件,这是有用的翻译(这样在你的字符串,你可以只是 \ n 作为行终止符,而不必担心特定的平台行终止符),但是当你编写一个二进制文件 \\\



*仅使用LF的NIX系统( \\\
)实际上不做任何翻译,但仍然可以指定正确的二进制/文本模式的可移植性/清晰度。



使用always <在C ++中的code> endl 是一个常见的错误, \\\
足以获得到平台特定的行终止符的转换。



什么 endl 超过 \\\
以刷新流缓冲器,这在一些有限的情况下是有用的(例如,在长时间操作之前在控制台上输出一些东西),但通常只是减慢IO(在控制台上通常是不明显的,但是在文件上)。我通常只使用 \\\
,并在实际需要刷新时添加 std :: flush 。 >




这关系到标准库;当处理其他库YMMV,你应该检查他们的文档,看看他们是否遵循标准C约定,或者他们需要字符串来包含平台特定的行终止符。


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:

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.

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?

解决方案

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 b into the fopen and ios::bin for C++ streams), is automatically translated to the line terminator of the current platform. That's why on Windows you can just write \n to any stream and it becomes "magically" CRLF into the file/on the console.

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 \n as line terminator without worrying about the specific platform line terminator), but when you're writing a binary file \n is just a byte like the others and should not be translated, otherwise you get corrupted data.

*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.

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

What endl does more than \n is 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 \n and add a std::flush when a flush is actually needed.


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.

这篇关于换行和回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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