简体   繁体   English

在C字符串输入中接受空格

[英]Accepting space in C string input

I am new to C language and working on a small program. 我是C语言的新手,正在开发一个小程序。

Actually I am trying to take string from the user using scanf. 实际上,我正在尝试使用scanf从用户那里获取字符串。 But whenever I enter string with space, program keeps on running infinite and I had to press stop button. 但是,每当我输入带空格的字符串时,程序便会无限运行,并且我必须按停止按钮。 I have seen examples online and I have used them as well but that give me a new error then. 我在网上看到了示例,也使用了它们,但是那给了我一个新的错误。

Here is my code 这是我的代码

struct student s1;
char input[MAX_NAME_SIZE];
printf("Enter name>");
scanf("%s",input);
if(strlen(input) > 10)
{
    int l;
    for(l = 0 ;l <  10;l++)
        s1.name[l] = input[l];
}

int error = 0;
do
{
    if(error == 1)
        printf("Invalid day. ");

    printf("Enter birthday: day>");
    scanf("%u",&s1.birthday.day);
    error = 1;
}while(s1.birthday.day < 1 || s1.birthday.day > 31); //checking the criteria 

I also have used scanf("%[^\\n]s,input) but it then skip the scanf and go to the second scanf. 我也使用过scanf(“%[^ \\ n] s,input),但是它随后跳过了scanf并转到了第二个scanf。

Please help 请帮忙

instead of using 而不是使用

scanf("%s",input);

you can use 您可以使用

gets(input);

It is a function of cstring header file and works same. 它是cstring头文件的功能,并且工作相同。 You may get the problem because scanf accepts only space delimited strings. 您可能会遇到问题,因为scanf仅接受以空格分隔的字符串。 gets accepts a complete single line, so it may solve your problem. gets接受一个完整的单行,因此可以解决您的问题。

Hopefully this will work! 希望这会起作用!

EDIT1: Just got it from one of the comments. EDIT1:刚从评论之一得到它。

use fgets() instead. 使用fgets()代替。 because gets() doesn't provide buffer overflow protection. 因为gets()不提供缓冲区溢出保护。

The problem, as I hinted about in my first comment, is that scanf only reads space delimited "words". 正如我在第一条评论中所暗示的那样,问题是scanf仅读取以空格分隔的 “单词”。

That means when you give a string with space in it as input, then scanf will read the first part up to the space, and leave the rest of the input in the buffer. 这意味着当您输入一个带有空格的字符串作为输入时, scanf会读取直到该空格的第一部分,并将其余的输入保留在缓冲区中。 Then when you do your input in the loop, scanf will see that it's not a number, and fail to extract the input from the buffer, leaving the input intact, so the next iteration of the loop scanf will attempt to read and parse the exact same input as the last iteration, leading to an infinite loop. 然后,当您在循环中进行输入时, scanf会发现它不是数字,并且无法从缓冲区中提取输入,而输入保持不变,因此循环的下一次迭代scanf将尝试读取并解析精确的与上次迭代相同的输入,导致无限循环。

There are also other problems with your code, like you not actually checking if scanf failed or not (follow the link to a scanf (and family) reference and read about what it returns), and not breaking out of the loop if error is set. 您的代码还存在其他问题,例如您实际上没有检查scanf失败(遵循指向scanf (和系列)引用的链接并了解返回的内容),并且如果设置了error ,也不会中断循环。

I would never recommend you to use scanf for input which is more than one word. 我绝不建议您使用scanf来输入多个单词。 getline() would be the best choice for entering the whole line (etc. Mike Myers 13/04/85) and it's pretty much safe. getline()将是输入整行的最佳选择(例如Mike Myers 13/04/85),它非常安全。 You also get null terminated string. 您还会得到以null结尾的字符串。

You can use this function for example: 您可以使用此功能,例如:

char *input(char *output) 
{
    char *Buffer = NULL;
    size_t size = 0;
    int count = 0;

    printf("%s", output);
    count = getline(&Buffer, &size, stdin);
    Buffer[count-1] = '\0';
    return Buffer;
}

In your case it would be best to use it like this: 在您的情况下,最好像这样使用它:

s1->name=input("Enter the name");
s1->birthday=input("Enter the birthday");

Or you can enter the whole line and use strtok() to parse it. 或者,您可以输入整行并使用strtok()对其进行解析。

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

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