简体   繁体   English

使用 getc 从控制台读取 \r(回车)与 \n(换行符)?

[英]Reading \r (carriage return) vs \n (newline) from console with getc?

I'm writing a function that basically waits for the user to hit "enter" and then does something.我正在写一个 function 基本上等待用户点击“输入”然后做一些事情。 What I've found that works when testing is the below:我发现在测试时有效的是:

#include <stdio.h>

int main()
{
        int x = getc(stdin);
        if (x == '\n') {
                printf("carriage return");
                printf("\n");
        }
        else {
                printf("missed it");
                printf("\n");
        }
}

The question I have, and what I tried at first was to do: if (x == '\r') but in testing, the program didn't catch me hitting enter.我的问题是,我最初尝试的是: if (x == '\r')但在测试中,程序没有发现我按下回车键。 The '\n' seems to correspond to me hitting enter from the console. '\n'似乎对应于我从控制台按回车键。 Can someone explain the difference?有人可以解释其中的区别吗? Also, to verify, writing it as if... == "\n" would mean the character string literal?另外,为了验证,把它写成if... == "\n"意味着字符串文字? ie the user would literally have to enter "\n" from the console, correct?也就是说,用户实际上必须从控制台输入"\n" ,对吗?

\n is the newline character, while \r is the carriage return. \n是换行符,而\r是回车符。 They differ in what uses them.它们的用途不同。 Windows uses \r\n to signify the enter key was pressed, while Linux and Unix use \n to signify that the enter key was pressed. Windows 使用\r\n表示按下回车键,而 Linux 和 Unix 使用\n表示按下回车键。

Thus, I'd always use \n because it's used by all;因此,我总是使用\n因为它被所有人使用; and if (x == '\n') is the proper way to test character equality. if (x == '\n')是测试字符相等性的正确方法。

The '\n' is the "Line Feed" and '\r' is the carriage return. '\n' 是“换行”,'\r' 是回车。 Different operating systems will handle new lines in a different way, such as不同的操作系统会以不同的方式处理新行,例如

Windows Windows

Expects a newline to be combination of two characters, '\r\n'.期望换行符是两个字符的组合,'\r\n'。

Linux\Unix and Modern Mac OS Linux\Unix 和现代 Mac OS

Uses a single '\n' for a new line.使用单个 '\n' 换行。

Classic Mac OS经典 Mac 操作系统

Uses a single '\r' for a new line.使用单个 '\r' 换行。

Basically, I would use if (x == '\n') as it is currently used by all modern operating systems.基本上,我会使用if (x == '\n')因为它目前被所有现代操作系统使用。

There are several levels of abstractions of representing a new line - the programming language runtime, the text editor and the OS.表示新行有多个抽象级别 - 编程语言运行时、文本编辑器和操作系统。 CR (carriage return) and LF (line feed) are two control charactors that are defined in ASCII. CR(回车)和 LF(换行)是 ASCII 中定义的两个控制字符。 Some other charator encoding might also define a "new line" charactor.其他一些字符编码也可能定义“换行”字符。 The Enter key on any keyboard conveys a meaning of "to the beginning of the next line".任何键盘上的回车键都传达了“到下一行的开头”的意思。 The keyboard decides how to map the Enter key to its corresponding control charactor or charactors.键盘决定如何将 map 回车键输入到其对应的一个或多个控制字符。 Some keyboard also differentiates Enter and Return key - let Enter key be new line and Return key be carriage return.有些键盘还区分 Enter 和 Return 键 - 让 Enter 键换行,Return 键回车。 In a standard ANSI keyboard, there's only Enter key, which is mapped to a carriage return charactor (13) in ASCII.在标准的 ANSI 键盘中,只有 Enter 键,它映射到 ASCII 中的回车符 (13)。 So this is the actual content that is sent to the OS by the device.所以这是设备发送给操作系统的实际内容。 However, different OS decides to interpret the Enter key differently.但是,不同的操作系统决定以不同方式解释 Enter 键。 So in Unix-like system, any carriage return is translated to a line feed charator (10 in ASCII) before handing to the program that receives the input.因此,在类 Unix 系统中,任何回车符在交给接收输入的程序之前都会被转换为换行符(ASCII 中的 10)。 And on Windows, a CR is translated to two charactors - a CR followed by a LF.在 Windows 上,一个 CR 被翻译成两个字符——一个 CR 后跟一个 LF。 However, you can set the input stream to be raw mode, in which case the program gets what the keyboard actually sends.但是,您可以将输入 stream 设置为原始模式,在这种情况下,程序会获取键盘实际发送的内容。 Now the editor comes into play.现在编辑器开始发挥作用了。 When the editor receives a CR from stdin in raw mode, it knows the CR corresponds to the Enter key on the keyboard (assumption about the keyboard) and it's supposed to display a new line on the screen.当编辑器以原始模式从标准输入接收到 CR 时,它知道 CR 对应于键盘上的 Enter 键(关于键盘的假设)并且它应该在屏幕上显示一个新行。 In the raw mode, it should call the write system call to output a CR + LF.在原始模式下,它应该调用写入系统调用 output 一个 CR + LF。 If the output stream is not in the raw mode, the text editor should output the OS-specific sequence such as LF on linux.如果 output stream 不在原始模式下,文本编辑器应该 output 操作系统特定的序列,例如 linux 上的 LF。

Finally, the language runtime can also interpret the new line in its own way.最后,语言运行时也可以用自己的方式解释新行。 For example, The C standard says When writing a file in text mode, '\n' is transparently translated to the native newline sequence used by the system, which may be longer than one character.例如,C 标准说当以文本模式写入文件时,'\n' 被透明地转换为系统使用的本机换行符序列,它可能长于一个字符。 When reading in text mode, the native newline sequence is translated back to '\n'.在文本模式下阅读时,本机换行符序列被转换回“\n”。 In binary mode, no translation is performed, and the internal representation produced by '\n' is output directly.在二进制模式下,不进行翻译,'\n'产生的内部表示直接为output。 Note that '\n' and '\r' are language-specific charactors that represent LF and CR respectively that are popular in C-like languages.请注意,'\n' 和 '\r' 是特定于语言的字符,分别表示 LF 和 CR,它们在类 C 语言中很流行。 But not every language has to use this notation.但并非每种语言都必须使用这种表示法。

For you second question, "\n" is '\n' followed by a '\0' terminator.对于第二个问题,“\n”是“\n”后跟一个“\0”终止符。 There is no way to enter a '\0' from the console.无法从控制台输入“\0”。

Also remember that if you type in 25 characters and Enter, the first getc will not return until all 25 characters have been typed in and you hit Enter.还要记住,如果您输入 25 个字符并按 Enter,第一个getc将不会返回,直到输入所有 25 个字符并按 Enter。 Reading a character at the time it is typed requires platform-specific code.在输入字符时读取字符需要特定于平台的代码。 Consequently, you might be better off just reading the entire line by performing fgets into a string, trimming the newline, and processing the input line as a whole.因此,您最好通过对字符串执行fgets 、修剪换行符并将输入行作为一个整体来处理来读取整行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM