gets()没有在C中输入

时间:2015-05-06 09:38:40

标签: c file-handling gets

好吧所以我正在编写这段简单的代码而且我只是C语言的初学者。这段代码按预期编译并运行但是当我到达必须输入地址的地步时,这段代码只是跳过输入,但是当我在那个地方使用scanf时,代码运行得很完美,任何人都可以帮助我吗?

来源

#include<stdio.h>
#include<conio.h>

void main()
{
    struct student
    {
        int rn;
        long int phn;
        char name[50];
        char add[50];
    }s;

    char ans, filename[15];

    FILE *fp;
    printf("\n Enter File Name : ");
    gets(filename);
    fp=fopen(filename, "w");

    again:
    printf("\n Enter a name of a student : ");
    gets(s.name);
    printf("\n Enter his roll number : ");
    scanf(" %d ", &s.rn);
    printf("\n Enter his address : ");
    gets( s.add);
    printf("\n Enter his phone Number : ");
    scanf("%ld", &s.phn);
    fprintf(fp, "%s\t%d\t%s\t%ld\n", s.name, s.rn, s.add, s.phn);
    printf("\n Want to keep next record ? : ");
    fflush(stdin);
    scanf("%c", &ans);
    if(ans=='y'||ans=='Y')
    goto again;
    fclose(fp);
    getch();
}

我想要一个详细的解释,任何人都可以帮助我吗?

0 个答案:

没有答案