Student Record-File Management Program in C

c-techaravind-student-record-img
                                              To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.

C program for append,list,search ,modify,delete a student record in a File.


#include<stdio.h>
#include<conio.h>
void append();
void list();
void search();
void modify();
void del();

struct student
{
int no;
char name[20];
};

void main()
{
int a;
char ch;
clrscr();
do{
printf("\nstudent DATABASE\n\n");
printf("1.Append student Record\n2.List student Record\n3.Modify student Record\n4.Delete student Record\n5.Search student Record\n Enter Choice : ");
scanf("%d",&a);
switch(a)
{
case 1:
append();
break;
case 2:
list();
break;
case 3:
modify();
break;
case 4:
del();
break;
case 5:
search();
break;
default :
printf("Invalid Choice!");
}
printf("\n More Actions ? (Y/N) :");
fflush(stdin);
scanf("%c", &ch);
}while(ch=='y'|| ch=='Y');
}

void append()
{
int i,n;
struct student e;
FILE *fp;
fp=fopen("student.dat", "a");
if(fp==NULL)
{
printf("File Creation Failed!");
exit(0);
}
printf("Enter the nos. of students : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the student Number : ");
scanf("%d", &e.no);
printf("Enter the student Name : ");
fflush(stdin);
gets(e.name);
printf("\n\n");
fwrite((char *)&e, sizeof(e), 1, fp);
}
fclose(fp);
}

void list()
{
int nofrec=0;
struct student e;
FILE *fp;
fp=fopen("student.dat", "rb");
if(fp==NULL)
{
printf("\n\tFile doesn’t exist!!!\TRY AGAIN");
exit(0);
}
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
nofrec++;
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n\n");
}
printf("Total number of records present are : %d", nofrec);
fclose(fp);
}

void modify()
{
int recno, nofrec=0;
char ch;
struct student e;
FILE *fp;
fp=fopen("student.dat", "rb+");
printf("Enter the student Number to modify : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
nofrec++;
if(e.no==recno)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
printf("Do you want to modify this record : ? (Y/N)");
fflush(stdin);
scanf("%c", &ch);
fseek(fp, ((nofrec-1)*sizeof(e)), 0);
if(ch=='Y'|| ch=='y')
{
printf("Enter the student No : ");
scanf("%d",&e.no);
printf("Enter the student Name : ");
fflush(stdin);
gets(e.name);
fwrite((char *)&e, sizeof(e), 1, fp);
printf("Record Modified");
}
else
printf("No modifications were made");
fclose(fp);
}
}
}
void del()
{
int recno;
char ch;
struct student e;
FILE *fp, *ft;
fp=fopen("student.dat", "rb");
ft=fopen("Temp.dat", "wb");
printf("Enter the student Number to delete : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
printf("Do you want to delete this record : ? (Y/N)");
fflush(stdin);
scanf("%c", &ch);
}
}
if(ch=='y'||ch=='Y')
{
rewind(fp);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(recno!=e.no)
{
fwrite((char *)&e, sizeof(e), 1, ft);
}
}
}
else
printf("No Record was deleted");
fclose(fp);
fclose(ft);
remove("student.dat");
rename("Temp.dat", "student.dat");
}


void search()
{
int s,recno;
char sname[20];
struct student e;
FILE *fp;
fp=fopen("student.dat", "rb");
printf("\n1.Search by Name\n2.Search by student No.\n Enter choice : ");
scanf("%d", &s);
switch(s)
{
case 1:
printf("Enter the student Name to Search : ");
fflush(stdin);
gets(sname);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(strcmp(sname,e.name)==0)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
}
}
break;
case 2:
printf("Enter the student Number to Search : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf("\nstudent Number : %d", e.no);
printf("\nstudent Name : %s",e.name);
printf("\n");
}
}
break;
}
}




0 comments:

Post a Comment

 
Etutos © 2010-2011