在同一个程序中使用scanf函数和与fgets? [英] Using scanf and fgets in the same program?

查看:279
本文介绍了在同一个程序中使用scanf函数和与fgets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做类似如下:

int main(void) {
char a,b,cstring;

printf("please enter something");
scanf("%c %c",&a,&b);
prinf("thanks, now some more");
fgets(cstring,35,stdin);

}

问题是,每当我从scanf函数输入第一个增值经销商,它跳到程序结束。我该怎么做多个输入?

Problem is that whenever I enter the first vars from scanf, it skips to the end of the program. How do I do multiple inputs?

推荐答案

第一个问题是, scanf()的读取两个字符,但不能换行之后。

The first problem is that the scanf() reads two characters, but not the newline afterwards.

这意味着与fgets()读取换行符和完成。

That means your fgets() reads the newline and finishes.

您是幸运的你的程序没有崩溃。你告诉与fgets(),它越来越的35个字符数组,而不是传递一个数组,你通过字符(不是字符的地址,偶数)。这也告诉我们,你是不是包括的#include<&stdio.h中GT; (不能使用 scanf()的可靠依据没有原型)的C标准为 scanf函数(的范围),你是不是有足够的警告编译来启用或不给予足够的重视你的编译器告诉你。

You are lucky your program is not crashing. You tell fgets() that it is getting an array of 35 characters, but rather than passing an array, you pass the character (not the address of a character, even). That also tells us you are not including #include <stdio.h> (you can't use scanf() reliably according to the C standard without a prototype for scanf() in scope), and you are not compiling with enough warnings enabled, or not paying enough attention to what your compiler is telling you.

您应该得到的编译器警告。例如,GCC 4.6.0说:

You should be getting compiler warnings. For example, GCC 4.6.0 says:

/usr/bin/gcc -g -I/Users/jleffler/inc -std=c99 -Wall -Wextra -Wmissing-prototypes \
     -Wstrict-prototypes -Wold-style-definition xxx.c
xxx.c: In function ‘main’:
xxx.c:7: warning: implicit declaration of function ‘prinf’
xxx.c:8: warning: passing argument 1 of ‘fgets’ makes pointer from integer without a cast

和,织补,但一直没有注意到 prinf()的printf()直到我试图链接和编译器揉我的鼻子在里面说不知道是什么 prinf()是!

And, darn it, I hadn't even noticed the spelling mistake of prinf() for printf() until I tried linking and the compiler rubbed my nose in it by saying "Don't know what prinf() is!".

如果你的编译器不至少给了第二次警告,让自己变成一个更好的编译器。

If your compiler doesn't at least give the second warning, get yourself a better compiler.

注释:

如何阻止它后,读取新行?

How do I stop it from reading the newline after it?

这个问题不从阅读新行停止 scanf()的;这个问题实际上就是如何使它读它,以便与fgets()获得在输入​​的下一行明确的运行。它实际上是适度棘手 - 一个原因我很少用 scanf()的(但我经常使用的sscanf()) 。这确实为简单的输入工作:

The problem is not stopping the scanf() from reading the newline; the problem is actually how to make it read it so that fgets() gets a clear run at the next line of input. It actually is modestly tricky - one reason why I seldom use scanf() (but I often use sscanf()). This does the job for simple inputs:

#include <stdio.h>
int main(void)
{
    char a,b,cstring[35];

    printf("please enter something");
    scanf("%c %c%*c", &a, &b);
    printf("thanks, now some more");
    fgets(cstring, 35, stdin);
    printf("OK: I got %c and %c and <<%s>>\n", a, b, cstring);
    return 0;
}

%* C ,而不会有任何分配给它(这是因为的 * )。然而,如果有之后的第二个非空的多个字符,这是没有任何帮助。我可能会写一个循环来读取到新行:

The %*c reads an extra character (a newline) without assigning it anywhere (that's because of the *). However, if there are multiple characters after the second non-blank, it is no help. I'd probably write a loop to read to the newline:

#include <stdio.h>
int main(void)
{
    char a,b,cstring[35];
    int c;

    printf("please enter something");
    scanf("%c %c", &a, &b);
    while ((c = getchar()) != EOF && c != '\n')
        ;
    printf("thanks, now some more");
    fgets(cstring, 35, stdin);
    printf("OK: I got %c and %c and <<%s>>\n", a, b, cstring);
    return 0;
}

注意的getchar()返回 INT ,而不是字符。这可靠地丢弃输入的第一行的一切。它还说明了如何呼应你读什么 - 调试的重要组成部分。

Note that getchar() returns an int and not a char. This reliably discards everything on the first line of input. It also shows how to echo what you read - an important part of debugging.

这篇关于在同一个程序中使用scanf函数和与fgets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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