Pregunta

I am developing a student management system in C, but I can't seem to accomplish the search function. Writing to a file works fine but searching by name is not working. Nothing happens when I enter the name and the program exits.

Here is my code so far:

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

struct student {
    int id;
    char name[10];
    int semester;
};

int main(void) {
    char name[10], c[100];
    struct student s;
    FILE *fp;
    int choice;

    printf("Enter 1 to Add record\nEnter 2 to Search \n");
    scanf("%d", &choice);

    switch(choice) {
        case 1:
            fp = fopen("std.txt", "a");
            printf("\nEnter id: ");
            scanf("%d", &s.id);
            fflush(stdin);
            printf("\nEnter name: ");
            scanf("%s", &s.name);
            fflush(stdin);
            printf("\nEnter semester: ");
            scanf("%d", &s.semester);
            fflush(stdin);

            fprintf(fp, "\nId: %d\tName: %s\tSemester: %d\n", s.id,s.name,s.semester);
            fclose(fp);
            break;

        case 2:
            fp=fopen("std.txt","r");
            printf("ENTER THE NAME ");
            scanf("%s",&name);
            while(feof(fp)) {
                if(strcmp(name, s.name) == 0) {
                    printf("\n\t%d\t %s\t %d", s.id, s.name, s.semester);
                    getch();
                    break;
                }
            }
            fclose(fp);
            getch();
            break;

        case 3:
            exit(1);
    }
    getchar();
} 
¿Fue útil?

Solución

You are not actually reading the line into the student structure. You probably meant to fgets or scanf each line in the file and load it into the struct student s.

Otros consejos

sample of use fwrite and fread

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

struct student {
    int id;
    char name[10];
    int semester;
};

int main(void) {
    char name[10], c[100];
    struct student s;
    FILE *fp;
    int choice;

while(1){
    printf("Enter 1 to Add record\nEnter 2 to Search \nEnter 3 to Exit\n");
    scanf("%d", &choice);

    switch(choice) {
        case 1:
            fp = fopen("std.txt", "ab");
            printf("\nEnter id: ");
            scanf("%d", &s.id);
            fflush(stdin);
            printf("\nEnter name: ");
            scanf("%s", &s.name);
            fflush(stdin);
            printf("\nEnter semester: ");
            scanf("%d", &s.semester);
            fflush(stdin);

            //fprintf(fp, "\nId: %d\tName: %s\tSemester: %d\n", s.id,s.name,s.semester);
            fwrite(&s, sizeof(s), 1, fp);
            fclose(fp);
            break;

        case 2:
            fp=fopen("std.txt","rb");
            printf("ENTER THE NAME ");
            scanf("%9s", name);
            getchar();
            while(fread(&s, sizeof(s), 1, fp)==1) {
                if(strcmp(name, s.name) == 0) {
                    printf("\n\t%d\t %s\t %d\n", s.id, s.name, s.semester);
                    break;
                }
            }
            fclose(fp);
            break;

        case 3:
            exit(1);
    }
}
    getchar();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top