C数据库程序无法找到第一个输入。为什么?

时间:2014-12-30 21:40:18

标签: c search input

我制作了一个程序,用于存储学生ID,姓名,姓氏和商标。该程序没有错误,但无法找到第一个输入。

input file;
23915746455 James   Doe 1   
23915741327 John    Doe 2   
23915741842 Henny   Fluffy  3

我的代码会在身份证和姓名搜索中找到约翰和亨尼,但却找不到詹姆斯。我的代码出了什么问题?第一个输入不会被搜索。

我的完整代码在这里;

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Student
{
    long long int id;
    char firstname[20];
    char lastname[20];
    int mark;
} student;

void
storeRecord()
{
    FILE *fp;

    printf("\nEnter Student Details:\n\nID number: ");
    scanf("%lld",&student.id);

    printf("\nName:");
    scanf("%19s",student.firstname);

    printf("\nSurname:");
    scanf("%19s",student.lastname);

    printf("\nMark(0 - 100 integer) : ");
    scanf("%d",&student.mark);

    fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
    if (fp == NULL)
        return;
    fprintf(fp, "\n%lld\t%s\t%s\t%d\t", 
        student.id, 
        student.firstname, 
        student.lastname, 
        student.mark);
    fclose(fp);

    printf("A student record has been added successfully...\n");
    getchar();
}

int
compareStudentsById(struct Student lhs, struct Student rhs)
{
    return (lhs.id == rhs.id);
}

int
compareStudentsByName(struct Student lhs, struct Student rhs)
{
    return (strcmp(lhs.firstname, rhs.firstname) == 0);
}

void
printStudent()
{
    printf("\nThe record is found.\n");
    printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
        student.id,
        student.firstname,
        student.lastname,
        student.mark
    );
}

void
searchStudent(int(*compare)(struct Student,struct Student), const char *const name, long long int id)
{
    FILE *fp;
    int   found;
    int   matches;


    if (name != NULL)
        printf("Searching record with Name = %s.\n", name);
    if (id != -1)
        printf("Searching record with ID   = %lld.\n", id);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
        &student.id, 
        student.firstname, 
        student.lastname, 
        &student.mark);

    do
    {
        struct Student other;

        if (name != NULL)
            strcpy(other.firstname, name);

        other.id = id;
        matches  = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);

        if (matches == 4)
            found = (compare(student, other) != 0);

    } while ((matches == 4) && (found == 0));

    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");

    getchar();
}

void
searchStudentByName()
{
    char studentname[20];

    printf("\nEnter student first name: ");
    scanf("%19s", studentname);

    searchStudent(compareStudentsByName, studentname, -1);
}

void
searchStudentById()
{
    long long int id;

    printf("\nEnter ID: ");
    scanf("%lld", &id);

    searchStudent(compareStudentsById, NULL, id);
}

int main()
{
    int choice;

    choice = 0;
    while (choice != 4)
    {
        printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM"); 
        printf("\n1 -> Store a new record in database\n");
        printf("2 -> Search a student record by Student First Name\n");
        printf("3 -> Search a student record by ID\n");
        printf("4 -> Quit Student Database");
        printf("\n\n");
        printf("Enter your choice : ");

        scanf("%d",&choice);
        switch(choice)
        {
        case  1:
            storeRecord();
            break;
        case 2:
            searchStudentByName();
            break;
        case 3:
            searchStudentById();
            break;
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

只需移除fscanf循环外的第一个while,即扫描文件的第一行。

您的代码副本已修复

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Student
{
    long long int id;
    char firstname[20];
    char lastname[20];
    int mark;
} student;

void
storeRecord()
{
    FILE *fp;

    printf("\nEnter Student Details:\n\nID number: ");
    scanf("%lld",&student.id);

    printf("\nName:");
    scanf("%19s",student.firstname);

    printf("\nSurname:");
    scanf("%19s",student.lastname);

    printf("\nMark(0 - 100 integer) : ");
    scanf("%d",&student.mark);

    fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
    if (fp == NULL)
        return;
    fprintf(fp, "\n%lld\t%s\t%s\t%d\t", 
        student.id, 
        student.firstname, 
        student.lastname, 
        student.mark);
    fclose(fp);

    printf("A student record has been added successfully...\n");
    getchar();
}

int
compareStudentsById(struct Student lhs, struct Student rhs)
{
    return (lhs.id == rhs.id);
}

int
compareStudentsByName(struct Student lhs, struct Student rhs)
{
    return (strcmp(lhs.firstname, rhs.firstname) == 0);
}

void
printStudent()
{
    printf("\nThe record is found.\n");
    printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
        student.id,
        student.firstname,
        student.lastname,
        student.mark
    );
}

void
searchStudent(int(*compare)(struct Student,struct Student), const char *const name, int id)
{
    FILE *fp;
    int   found;
    int   matches;


    if (name != NULL)
        printf("Searching record with Name = %s.\n", name);
    if (id != -1)
        printf("Searching record with ID   = %d.\n", id);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    do
    {
        struct Student other;

        if (name != NULL)
            strcpy(other.firstname, name);

        other.id = id;
        matches  = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);

        if (matches == 4)
            found = (compare(student, other) != 0);

    } while ((matches == 4) && (found == 0));

    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");

    getchar();
}

void
searchStudentByName()
{
    char studentname[20];

    printf("\nEnter student first name: ");
    scanf("%19s", studentname);

    searchStudent(compareStudentsByName, studentname, -1);
}

void
searchStudentById()
{
    int id;

    printf("\nEnter student first name: ");
    scanf("%d", &id);

    searchStudent(compareStudentsByName, NULL, id);
}

int main()
{
    int choice;

    choice = 0;
    while (choice != 4)
    {
        printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM"); 
        printf("\n1 -> Store a new record in database\n");
        printf("2 -> Search a student record by Student First Name\n");
        printf("3 -> Search a student record by ID\n");
        printf("4 -> Quit Student Database");
        printf("\n\n");
        printf("Enter your choice : ");

        scanf("%d",&choice);
        switch(choice)
        {
        case  1:
            storeRecord();
            break;
        case 2:
            searchStudentByName();
            break;
        case 3:
            searchStudentById();
            break;
        }
    }
    return 0;
}