简体   繁体   English

C编程:EOF作为一个角色

[英]C Programming: EOF as a character

When programming C for the command console, what happens when you have a function that tries to use SCANF to ask user input for a CHAR variable, and the user types CTRL + Z (EOF) and hits enter? 当为命令控制台编程C时,当你有一个试图使用SCANF向用户输入CHAR变量的函数,并且用户输入CTRL + Z (EOF)并命中输入时会发生什么?

For example: 例如:

char promptChar()
{
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);
    return c;
}

If the user types CTRL + Z and hits enter, what will promptChar() return? 如果用户键入CTRL + Z并按下Enter键,那么promptChar()会返回什么? Because if I understand EOF, it's an int. 因为如果我理解EOF,它就是一个int。

First things first: 首先要做的事情:

SCANF is not defined by the language. SCANF不是由语言定义的。
CHAR is not defined by the language. CHAR不是由语言定义的。

Ok, with that out of the way ... 好的,开始了......

The scanf() function returns an integer. scanf()函数返回一个整数。 That integer is the number of input items assigned or the value of the macro EOF if an input failure occurs before the first conversion. 如果在第一次转换之前发生输入故障,则该整数是指定的输入项数或宏EOF的值。
You didn't check the return value of the scanf() call, so you have no idea what happened. 你没有检查scanf()调用的返回值,所以你不知道发生了什么。 Everything might have worked ok, or the input stream might have ended before the first conversion, or (not for %c) there might have been a conversion failure. 一切都可能正常,或者输入流可能在第一次转换之前结束,或者(不是%c)可能存在转换失败。

Test the return value of scanf() . 测试scanf()的返回值。 Indeed, always test the return value of all <stdio.h> functions . 实际上, 总是测试所有<stdio.h>函数的返回值

char ch;
int result = scanf("%c", &ch);
if (result == 1) /* all ok */;
else if (result == 0) /* conversion failure: value of `ch` is indeterminate */;
else if (result == EOF) /* input failure; value of `ch` is indeterminate */;

When the result of the scanf() call is EOF , if you want more information about the reason for input failure, you can use feof() and/or ferror() . scanf()调用的结果是EOF ,如果您想了解有关输入失败原因的更多信息,可以使用feof()和/或ferror()

else if (result == EOF) {
    if (feof(stdin)) {
        /* no data in input stream */
    }
    if (ferror(stdin)) {
        /* error if input stream (media ejected? bad sector? ...?)
    }
}

To answer your question: what will promptChar() return? 回答你的问题: what will promptChar() return?

It will return an indeterminate value of type char. 它将返回char类型的不确定值。
You could follow the example of library function that deal with characters and return an int from promptChar() . 您可以按照处理字符的库函数示例并从promptChar()返回一个int。 That would be the value of the character read cast to unsigned char or a negative int ( EOF ) in case of error. 这将是对unsigned char进行字符读取的值或出现错误时为负int( EOF )的值。 Read the description for fgetc() , for instance. 例如,阅读fgetc()的描述。

From the Linux scanf(3) manpage: 从Linux scanf(3)页:

"The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3) ) is set, and errno is set indicate the error." “如果在第一次成功转换或匹配失败发生之前达到输入结束,则返回值EOF 。如果发生读取错误,也会返回EOF ,在这种情况下,流的错误指示符(参见ferror(3) )已设置,并且设置了errno表示错误。“

Note that this passage concerns the return value of scanf , not the result parameters. 请注意,此段落涉及scanf的返回值,而不是结果参数。

It depends on the command shell you are using, but you really shouldn't be designing any program to expect to read control characters from an interactive prompt. 这取决于您正在使用的命令shell,但您实际上不应该设计任何期望从交互式提示中读取控制字符的程序。

Most command shells will intercept some of the control characters and use them to tell the shell to do things. 大多数命令shell将拦截一些控制字符并使用它们来告诉shell执行操作。 For instance ctrl-s and ctrl-q often start and stop the shell's diplay of output characters. 例如ctrl-s和ctrl-q经常启动和停止shell的输出字符显示。 ctrl-z on some shells will actually be taken as a command to shut the shell down. 某些shell上的ctrl-z实际上将被视为关闭shell的命令。

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

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