Saturday, February 14, 2015

C program to write student record to a binary file


C program to write student record to a binary file

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

struct student
{
int rollno;
char *name;
int m1,m2,m3;
}s1;

void main()
{
FILE *fp;
clrscr();
fp=fopen("Record.dat","wb"); //opening binary file in writing mode
if(fp==NULL)
{
printf("File could not open");
exit(0);
}

printf("Enter student details
");

printf("Roll No:");
scanf("%d",&s1.rollno);
printf("Name:");
scanf("%s",s1.name);
printf("Marks in three subjects:");
scanf("%d%d%d",&s1.m1,&s1.m2,&s1.m3);

fwrite(&s1,sizeof(s1),1,fp);     //writing to binary file
printf("
Record has been added successfully...!!");

fclose(fp);
getch();
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.