Write a Menu Driven C Program for Student Details using File Handling

By | 03.12.2016

C program for Student Details using File Handling


Write a Menu Driven C Program for Student Details using File Handling. Here’s a Simple Program C Program for Student Details using File Handling in C Programming Language.

This program stores the information (ID, name, marks1, marks2, marks3) of a student and displays it on the screen using structures.


Below is the source code for  Menu Driven C Program for Student Details using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


#include <stdio.h>
typedef struct
{
    int id;
    char name[25];
    int m1,m2,m3;
}STD;
STD s;
void display(FILE *);
int search(FILE *,int);
void main()
{
    int i,n,id_key,opn;
    FILE *fp;
    printf(" How many Records do you want? ");
    scanf("%d",&n);
    fp=fopen("stud.dat","w");
    for(i=0;i<n;i++)
    {
        printf("Read the Info for Student: %d (Id,name,marks1,marks2,marks3) \n",i+1);
        scanf("%d%s%d%d%d",&s.id,s.name,&s.m1,&s.m2,&s.m3);
        fwrite(&s,sizeof(s),1,fp);
    }
    fclose(fp);
    fp=fopen("stud.dat","r");
    do
    {
        printf("\nPress :: \n 1- Display\n 2- Search\n 3- Exit\n Your Option ? ");
        scanf("%d",&opn);
        switch(opn)
        {
        case 1: printf("\n Student Records in the File \n");
            display(fp);
            break;
        case 2: printf(" Read the ID of the student to be searched ?");
            scanf("%d",&id_key);
            if(search(fp,id_key))
            {
                printf("Success ! Record found in the file\n");
                printf("%d\t%s\t%d\t%d\t%d\n",s.id,s.name,s.m1,s.m2,s.m3);
            }
            else
                printf(" Failure!! Record with id %d not found\n",id_key);
            break;
        case 3:  printf(" Exit!! Press a key . . .");
            getch();
            break;
        default:  printf(" Invalid Option!!! Try again !!!\n");
            break;
        }
    }while(opn != 3);
    fclose(fp);
}
   /* End of main() */


void display(FILE *fp)
{
    rewind(fp);
    printf("ID\tName\tMarks1\tMarks2\tMarks3\n");
    while(fread(&s,sizeof(s),1,fp))

        printf("%d\t%s\t%d\t%d\t%d\n",s.id,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp, int id_key)
{
    rewind(fp);
    while(fread(&s,sizeof(s),1,fp))
        if( s.id == id_key) return 1;
    return 0;
}

OUTPUT : :


How many Records do you want? 4
Read the Info for Student: 1 (Id,name,marks1,marks2,marks3)
1
John
50
77
88
Read the Info for Student: 2 (Id,name,marks1,marks2,marks3)
2
Smith
76
89
90
Read the Info for Student: 3 (Id,name,marks1,marks2,marks3)
3
Jackman
76
66
87
Read the Info for Student: 4 (Id,name,marks1,marks2,marks3)
4
Rambo
45
53
62

Press ::
 1- Display
 2- Search
 3- Exit
 Your Option ? 1

 Student Records in the File
ID      Name    Marks1  Marks2  Marks3
1       John    50      77      88
2       Smith   76      89      90
3       Jackman 76      66      87
4       Rambo   45      53      62

Press ::
 1- Display
 2- Search
 3- Exit
 Your Option ? 2
 Read the ID of the student to be searched ?3
Success ! Record found in the file
3       Jackman 76      66      87

Press ::
 1- Display
 2- Search
 3- Exit
 Your Option ? 1

 Student Records in the File
ID      Name    Marks1  Marks2  Marks3
1       John    50      77      88
2       Smith   76      89      90
3       Jackman 76      66      87
4       Rambo   45      53      62

Press ::
 1- Display
 2- Search
 3- Exit
 Your Option ? 3
 Exit!! Press a key . . .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

4.5 2 votes
Article Rating
Category: Advance Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Faraz

If I open the .dat or .TXT file in a text editor then it is not showing me the entered data and instead showing some random characters.
Plz help me with this.

bush

same

bush

Please reply admin