Çscanf()的和与fgets()的问题 [英] C scanf() and fgets() problem

查看:135
本文介绍了Çscanf()的和与fgets()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取用户输入并存储为包括空格的字符串。我做了一个解决方案搜索,并指出与fgets()或scanf(%[^ \\ n],STR)。但是,这两种解决方案给我一个错误。

I'm trying to read user input and store it as a string including the whitespace. I did a search for a solution and was pointed to fgets() or scanf(%[^\n], str). But both these solutions give me an error.

这是我有:

//MAX_CHARACTERS is set to 30

 scanf("%d", &input);
 if (input == 1){
        int pr;
        char na[MAX_CHARACTERS+1];
        printf("\nEnter the name: ");
        scanf("%[^\t\n]", &na);
        while (strlen(na)>MAX_CHARACTERS){
            printf("\nName is too long, enter new name: ");
            scanf("%[^\t\n]", &na);
        }// end na check
        printf("\nEnter priority: ");
        scanf("%d", &pr);
        while (pr>MAX_PRIORITY || pr <MIN_PRIORITY){
            printf("\nBad priority, enter new priority (0-100): ");
            scanf("%d", &pr);
        }//end pr check

如果我用%S %的所有实例,它工作正常[^ \\ t \\ n] ,但当我使用%[^ \\ t \\ n]

It works fine if I use %s in all instances of %[^\t\n] but when I use %[^\t\n] or

与fgets(NA,30,标准输入),它跳过第一个scanf函数的名称和去直接输入优先。然后,当我打印,我有我所输入的优先级的空白名称。

fgets(na, 30, stdin), it skips the first scanf for name and goes straight to "Enter priority: ". Then when I print, I have a blank name with whatever priority I entered.

编辑:对不起,第一个scanf函数缺少的报价是一个错字。没有问题的原因。我在'1'输入用于第一scanf的(%d个,输入)。

Sorry, the missing quotes on the first scanf is a typo. Not a cause of the problem. I typed in '1' for the first scanf("%d", input).

固定它

因为它不会让我发布一个答案,

Since it won't let me post an answer yet,

有人想通了。柜面任何人的兴趣依旧,问题是第一个scanf()的。

Someone figured it out. Incase anyone's still interested, the problem was the first scanf().

scanf("%d", &input);

它留在缓冲器中的\\ n。第二个走的是\\ n和阅读它作为一个输入,所以它被跳过。

It is leaving a \n in the buffer. The second one is taking the \n and reading it as an input so it gets skipped.

把一个

fflush(stdin); //right after the if statement seems to have fixed the issue. 

感谢大家的帮助。

Thanks for everyone's help.

推荐答案

这是用 scanf()的采取用户输入的问题,它没有作出处理字符串或任何不好的投入。 scanf()的是为了读取格式的输入(有严格的特定格式例如,文件)。当从用户输入服用,输入可以畸形并引起问题。的你应该使用与fgets()来读取输入和你自己解析它。

This is the problem with using scanf() to take in user input, it's not made to handle strings or any bad inputs. scanf() is meant to read formatted input (e.g., files with a strict and specific format). When taking in input from the user, input could be malformed and causes problems. You should use fgets() to read the input and parse it yourself.

要回答为什么第二个 scanf()的呼叫会跳过,这是因为第一次调用只消耗了被输入的数字。然而从pressing换行<大骨节病>输入保留在输入缓冲区,并反过来,第二次调用的输入。为了解决这个问题,你会想从输入缓冲器删除换行符。要做到这一点,最安全的方式是使用的getchar(),直到换行符被读取,然后继续你的程序。并避免使用 fflush(标准输入)模式,这不是安全的,并可能导致更多的问题比它的价值。

To answer why the second scanf() call gets skipped, it's because the first call only consumes the digits that gets inputted. However the newline from pressing Enter remains in the input buffer and is in turn, the input of the second call. To fix this, you'd want to remove that newline character from the input buffers. The safest way to do that would be to use getchar() until the newline character is read, then continue with your program. And avoid using the fflush(stdin) pattern, that's not safe and can cause more problems than it's worth.

下面是你可以用它来读取字符,直到一个换行符被读取一个简单的函数:

Here's a simple function you can use to read characters until a newline is read:

void clear_newlines(void)
{
    int c;
    do
    {
        c = getchar();
    } while (c != '\n' && c != EOF);
}

任何调用 scanf()的调用后,这个你正在处理非字符串数据。

Call this after any call to scanf() that you're processing non-string data.

这篇关于Çscanf()的和与fgets()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆