简体   繁体   English

C:您如何处理没有用户输入的scanf?

[英]C: How do you handle for no user input for scanf?

Dear fellow Stackoverflowers, 亲爱的Stackoverflowers,

How do you handle for 0 user input? 您如何处理0个用户输入?

For example, if the user enters " " or just presses ENTER, how do you handle for that? 例如,如果用户输入“”或仅按Enter,您将如何处理?

#include <stdio.h>
#include <string.h>

int main() {

   printf("> \n");
   char string[129];
   int i = 0, length = 0, flag = 0;

   printf("Input a string: ");
   scanf("%128s", string);
   if(strlen(string) != 0) {
      printf("%s\n", string);
   } else {
      printf("Please enter at least one argument.");
   }
}

Quoting C11 , chapter §7.21.6.2, fscanf() , regarding the %s conversion specifier, 引用C11 ,第§7.21.6.2章, fscanf()关于%s转换说明符,

s Matches a sequence of non-white-space characters. s匹配一系列非空格字符。

and regarding the steps for execution of a conversion specifier 以及有关执行转换说明符的步骤

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [ , c , or n specifier. 除非规范包括[cn说明符,否则将跳过输入的空白​​字符(由isspace函数指定)。

So, unless a non-whitespace character is there in the input stream, it will wait. 因此,除非输入流中存在非空白字符,否则它将等待。 No matching will take place. 不会进行匹配

Also, it's very important that you check the return value of scanf() and family to ensure that the scanning is success. 另外,检查scanf()和family的返回值以确保扫描成功非常重要。

That said, int main() should be int main(void) at least to conform to the standards. 也就是说, int main()应该至少是int main(void)以符合标准。

Achieving this with scanf() is probably impossible and I have no interest in finding out whether it's possible because this solution 使用scanf()实现此目标可能是不可能的,并且我对找出是否可行没有兴趣,因为此解决方案

#include <stdio.h> /* For fgets(), fprintf() and printf() */
#include <stdlib.h> /* For EXIT_FAILURE */
#include <ctype.h> /* For isspace() */

int
please_enter_at_least_one_argument()
{
    fprintf(stderr, "Please enter at least one argument\n");
    return EXIT_FAILURE;
}

int
main(void)
{
    char string[130];

    printf("> \n");
    printf("Input a string: ");
    if (fgets(string, sizeof(string), stdin) == NULL)
        return please_enter_at_least_one_argument();
    else
    {
        char *pointer;

        pointer = string;
        while (isspace((unsigned char) *pointer) != 0)
            pointer++;
        if (*pointer == '\0')
            return please_enter_at_least_one_argument();
        printf("%s\n", string);
    }
    return 0;
}

solves the problem and is simple very easy to understand. 解决了这个问题,并且非常简单易懂。

Please note that the first please_enter_at_least_one_argument() might not be correct because fgets() might return NULL if you press Ctrl + D ( Or on windows Ctrl + Z ) and also when an error occurs. 请注意,第一个please_enter_at_least_one_argument()可能不正确,因为如果您按Ctrl + D或者在Windows Ctrl + Z上 )以及发生错误时, fgets()可能返回NULL But to find out how to handle that you should probably read man fgets(3) . 但是要了解如何处理该问题,您可能应该阅读man fgets(3)

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

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