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

标签 c search input

我制作了一个程序,其中存储学生的 ID、姓名、姓氏和分数。该程序没有错误,但找不到第一个输入。

即。

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

我的代码将在 ID 和姓名搜索中找到 John 和 Henny,但不会找到 James。我的代码有什么问题吗?将不会搜索第一个输入。

我的完整代码在这里;

#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;
}

最佳答案

只需删除 while 循环之外的第一个 fscanf 即可,它会扫描文件的第一行。

已修复代码的副本

#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;
}

关于C 数据库程序找不到第一个输入。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712964/

相关文章:

java - : "Java Search". -1 期间发生内部错误

javascript - AJAX:延迟在表单字段中输入搜索

c - unix网络编程select函数总是返回1(resoved)

java - 创建一个图表,以便我可以在其上应用统一成本搜索

c - 我们应该在哪个内存段中找到C中函数的内存地址

ios - MPMediaPickerController 在 iPad 上缺少搜索功能

javascript - JQuery UI 自动完成功能不搜索所有数组

PHP "php://input"与 $_POST

c - 尝试将蓝牙套接字绑定(bind)到 PSM 17 和 19 在 MeeGo 上返回 EACCESS

c - 如何打印 2 个字符的 ASCII 值?