Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

c - Using scanf and fgets in the same program?

I need to do something like the following:

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);

}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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.

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

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.


Comment:

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

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>>
", a, b, cstring);
    return 0;
}

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 != '
')
        ;
    printf("thanks, now some more");
    fgets(cstring, 35, stdin);
    printf("OK: I got %c and %c and <<%s>>
", a, b, cstring);
    return 0;
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...