通过ID在文本文件中搜索名称

时间:2018-10-21 16:37:21

标签: c search scanf

我有一个带有两行的文本文件:

Voicu; Catalin; 1234; 2.32

Diana; Cadar; 2345; 1.01

我正在尝试让我的程序在.txt文件中搜索ID(1234或2345),并将其与名称匹配。下面是我尝试制作的代码,但是在我在第一个scanf中写入ID后,它在.txt中找不到ID,出现的第一件事是“ else”行。

    void alegereStudent(struct student *stud)
{

    FILE *scStudenti = fopen("studenti.txt", "a");
    char *error;
    int i=0,n;
    if(scStudenti==NULL)
    {
            error = strerror(errno);
            printf("Fisierul nu poate fi creat: \n %s", error);
    }

    printf("\nIntroduceti Numele: ");
    scanf("%39s", stud->numeStudent);

    printf("Introduceti Prenumele: ");
    scanf("%39s", stud->prenumeStudent);

    printf("Introduceti ID-ul studentului: ");
    scanf("%d", &stud->idStudent);

    printf("Introduceti Media de admitere: ");
    scanf("%f", &stud->medieAdmitere);

    fprintf(scStudenti,"<%s>;", stud->numeStudent);
    fprintf(scStudenti,"<%s>;", stud->prenumeStudent);
    fprintf(scStudenti,"<%d>;", stud->idStudent);
    fprintf(scStudenti,"<%.2f>\n", stud->medieAdmitere);
    fclose(scStudenti);

}

//

 void optiunea_2()
{
    printf("\n1.Despre studenti.\n");
    printf("2.Despre profesori.\n");
    printf("3.Revenire la meniu principal.\n");
    printf("\nAlegeti o optiune: ");
    scanf("%d",&alegere_opt2);

    switch(alegere_opt2)
    {
        case 1: while(&stud!=NULL)

        {

            FILE *scStudenti = fopen("studenti.txt", "r");
            char *error;
            int cautareID;

                if(scStudenti==NULL)
                {
                    error = strerror(errno);
                    printf("Fisierul nu poate fi creat: \n %s", error);
                }

            printf("\nIntroduceti ID-ul studentului: ");
            scanf("%d", &cautareID);
            fscanf(scStudenti,"%d",&stud->idStudent);
            if(cautareID==&stud->idStudent)
            {
            printf("Numele Studentului este %s %s",&stud->numeStudent, &stud->prenumeStudent);
            }
            else
                printf("\nAcest ID nu exista!\n");
                optiunea_2();


        }break;

2 个答案:

答案 0 :(得分:1)

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

int main(void)
{
    char const *filename = "test.txt";
    FILE *input = fopen(filename, "r");
    if (!input) {
        fprintf(stderr, "Couldn't open \"%s\" for reading!\n\n", filename);
        return EXIT_FAILURE;
    }

    char name[101];
    char surname[101];
    int id;
    double value;
    while (fscanf(input, "%100[^;];%100[^;];%d;%lf%*[\n]", name, surname, &id, &value) == 4) {
        if (id == 2345) {
            printf("Found \"%s %s\" by id %d\n\n", name, surname, id);
            break;
        }
    }
}

答案 1 :(得分:1)

fprintf(scStudenti,"<%s>;", stud->numeStudent);
fprintf(scStudenti,"<%s>;", stud->prenumeStudent);
fprintf(scStudenti,"<%d>;", stud->idStudent);
fprintf(scStudenti,"<%.2f>\n", stud->medieAdmitere);
fclose(scStudenti);

这使格式化更加困难。输出文件将包含:

<first>;<last>;<1234>;<0.5>

您要从一个新文件开始并简化输出以编写:

first;last;1234;0.5

不要总是将&放在整数前面。只需使用:

if(cautareID == stud->idStudent)
{
...
}

示例:

const char *filename = "studenti.txt";

void alegereStudent()
{
    struct student stud;
    FILE *scStudenti = fopen(filename, "a");
    if(scStudenti == NULL)
    {
        printf("error\n");
        return;
    }

    printf("\nIntroduceti Numele: ");
    scanf("%39s", stud.numeStudent);

    printf("Introduceti Prenumele: ");
    scanf("%39s", stud.prenumeStudent);

    printf("Introduceti ID-ul studentului: ");
    scanf("%d", &stud.idStudent);

    printf("Introduceti Media de admitere: ");
    scanf("%f", &stud.medieAdmitere);

    fprintf(scStudenti, "%s;", stud.numeStudent);
    fprintf(scStudenti, "%s;", stud.prenumeStudent);
    fprintf(scStudenti, "%d;", stud.idStudent);
    fprintf(scStudenti, "%.2f\n", stud.medieAdmitere);
    fclose(scStudenti);
}

void find_by_id(int id)
{
    FILE *fin = fopen(filename, "r");
    if(!fin)
        return;

    int count = 0;
    struct student stud;
    while(fscanf(fin, "%39[^;];%39[^;];%d;%f\n",
        stud.numeStudent, stud.prenumeStudent, &stud.idStudent, &stud.medieAdmitere) == 4)
    {
        if(id == stud.idStudent)
        {
            printf("found %s %s\n", stud.numeStudent, stud.prenumeStudent);
        }
        count++;
    }

    if(count == 0)
        printf("failed to read any record. Start with a new file.\n");
}

int main(void)
{
    alegereStudent();
    find_by_id(1234);
    return 0;
}

如果添加了<>个字符,然后按照该格式进行读取,则必须从字符串中删除多余的<>个字符。

void alegereStudent()
{
    struct student stud;
    ...
    fprintf(scStudenti, "<%s>;", stud.numeStudent);
    fprintf(scStudenti, "<%s>;", stud.prenumeStudent);
    fprintf(scStudenti, "<%d>;", stud.idStudent);
    fprintf(scStudenti, "<%.2f>\n", stud.medieAdmitere);    
    fclose(scStudenti);
}

void find_by_id(int id)
{
    ...
    struct student stud;
    while(fscanf(fin, "%39[^;];%39[^;];<%d>;<%f>\n",
        stud.numeStudent, stud.prenumeStudent, &stud.idStudent, &stud.medieAdmitere) == 4)
    {
        if(id == stud.idStudent)
        {
            char *p;
            size_t i;

            //remove the extra <> characters
            p = stud.numeStudent;
            if (strlen(p) > 1)
            {
                for(i = 1; i < strlen(p) - 1; i++)
                    p[i - 1] = p[i];
                p[i - 1] = 0;
            }

            //remove the extra <> characters
            p = stud.prenumeStudent;
            if (strlen(p) > 1)
            {
                for(i = 1; i < strlen(p) - 1; i++)
                    p[i - 1] = p[i];
                p[i - 1] = 0;
            }

            printf("found %s %s\n", stud.numeStudent, stud.prenumeStudent);
        }
        count++;
    }

    if(count == 0)
        printf("failed to read any record. Start with a new file.\n");
}