Write a C program to Read and Write student records to a file

By | 01.12.2016

Read and Write student records to a file


Write a C program to Read and Write student records to a file. Here’s simple program to Read and Write student records to a file in C Programming Language.


Below is the source code for C program to Read and Write student records to a file which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C program to Read and Write student records to a file  */

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

struct student
{
int rollno;
char name[50];
int m1,m2,m3;
}s1;

int main()
{
    FILE *fp;
    fp=fopen("C:\\Users\\acer\\Documents\\file4.txt","w");
    if(fp==NULL)
    {
    printf("File could not open");
    exit(0);
    }

    printf("Enter student details :: \n");
    printf("\nName :: ");
    scanf("%s",&s1.name);
    printf("Roll No :: ");
    scanf("%d",&s1.rollno);
    printf("Marks1 :: ");
    scanf("%d",&s1.m1);
    printf("Marks2 :: ");
    scanf("%d",&s1.m2);
    printf("Marks3 :: ");
    scanf("%d",&s1.m3);

    fwrite(&s1,sizeof(s1),1,fp);
    printf("\nRecord has been added successfully !!\n");
    fclose(fp);

    return 0;

}

OUTPUT : :


/*  C program to Read and Write student record to a file  */

Enter student details ::

Name :: CodezClub
Roll No :: 1331
Marks1 :: 87
Marks2 :: 89
Marks3 :: 95

Record has been added successfully !!

Process returned 0

Above is the source code for C program to Read and Write student records to a file which is successfully compiled and run on Windows System.The Output of the program is shown above .

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 up to you in short interval.


Thanks for reading the post….

4 3 votes
Article Rating
Category: C Programming File Handling 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

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Bryce Davis

The numbers are not showing

prasanth.m

i have very use your website,thanking you