简体   繁体   English

接受C中的空字符串

[英]Accepting empty strings in C

My program can either accept a number of any length or empty input. 我的程序可以接受任意长度或空输入。 However, if the input is empty (space or newline), the program continues to wait for an input. 但是,如果输入为空(空格或换行符),程序将继续等待输入。 I also tried fgets but if space/newline is pressed, it still waits for more input that is not a space/newline before closing. 我也尝试过fgets但是如果按下空格/换行符,它仍然会在关闭之前等待更多不是空格/换行符的输入。

Simpified code: 简化代码:

#include <stdio.h>
main()
{
    int num;
    scanf("%i",&num);
    printf("%i",num);
}

Input: 输入:

363792 

Output: 输出:

363792

Desired: 期望:

Input: 输入:

Output: 输出:

I'm new to C and am having a very hard time accomplishing this. 我是C的新手,我很难完成这项任务。

What tried using fgets: 尝试使用fgets:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
    int n;
    char s[20];

    fgets(s,20,stdin);
    n = atoi(s);

    printf("%i",n);
}

Edit: Turns out I was not compiling the code right. 编辑:结果我没有正确编译代码。 So every time I tried to make changes, it just looked at the original code using scanf. 所以每当我尝试进行更改时,它只是使用scanf查看原始代码。

I also tried fgets but if space/newline is pressed, it still waits for more input that is not a space/newline before closing. 我也尝试过fgets但是如果按下空格/换行符,它仍然会在关闭之前等待更多不是空格/换行符的输入。

Firstly fgets will work in this case. 首先, fgets将在这种情况下工作。 If you had shown us what exactly you tried to do using fgets() then answer to this question would have much narrow or very specific. 如果您向我们展示了您使用fgets()确切尝试做什么,那么回答这个问题会有很多甚至非常具体。

I tried to do the same using fgets() and below is the code snippet. 我尝试使用fgets()执行相同操作,以下是代码段。

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

int main(int argc, char *argv[])
{  
    char string[100];
    printf("Enter a number: ");

    fgets (string, 100, stdin);
    /* Here remove  the trailing new line char; not required unless you are trying to print the string */
    string[strlen(string) - 1] = '\0';

    printf("Num is %d\n", atoi(string));

    return 0;
}

If there is no input or just enter or space and enter, then number printed will be zero and fgets() will not wait until you enter a valid number. 如果没有输入或只输入或空格并输入,则打印的数字将为零,并且fgets()将不会等到您输入有效数字。

Also check this on why you shouldn't use gets . 另外,请检查为什么不应该使用gets Do not use gets() 不要使用gets()

According to definition of scanf() 根据scanf()的定义

The function will read and ignore any whitespace characters encountered before the next non-whitespace character. 该函数将读取并忽略在下一个非空白字符之前遇到的任何空格字符。

So you can not get your desired result using scanf() . 因此,使用scanf()无法获得所需的结果。 As gets() is not safe and it misbehaves badly . 因为gets()不安全而且行为不端 you can use fgets() to get input and convert it to integer if you need using atoi() . 如果需要使用atoi()可以使用fgets()获取输入并将其转换为整数。

Here sample code you can try: 您可以尝试以下示例代码:

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
    int num;
    char ch[100];
    fgets(ch, sizeof ch, stdin);
    if (strlen(ch)>1&& ch[strlen(ch)-1] == '\n')
    {
        ch[strlen(ch)-1] = '\0';
        num = atoi(ch);
        printf("%i\n", num);
    }
}

You should read the input character by character and stop reading if the read character is not a number: 您应该逐个读取输入字符,如果读取的字符不是数字则停止读取:

char ch;
while(1)
{
    ch=getchar();
    if(isdigit(ch))
    {
        putchar(ch);
    }
    else
    {
        break;
    }
}

alright, you do not understand what scanf() does, it goes and scans entire input string and looks for special characters like %d, %f, %s and until it does not get what is in string it will wait. 好吧,你不明白scanf()做什么,它去扫描整个输入字符串并查找特殊字符,如%d,%f,%s,直到它没有得到它将等待的字符串。

scanf("%d %d", a, b);

will get done only when you enter 2 integer values, if you press space it does not count as integer value so it will ignore it. 只有当你输入2个整数值时才会完成,如果你按空格它不算作整数值,所以它会忽略它。

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

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