Primitive operations on Sequential file in C language
Implement all primitive operations on Sequential file in C
Primitive operations on Sequential file in C language
#include #include #include typedef struct stud { int roll; char name[20]; struct add { int hno; char city[10]; }add; char clas[4]; int mark; }stud; stud alloc(stud ); void insert(char [20]); void create(); void display(); void searchnr(char [20]); void searchcr(char [20]); void modify(); void delet(); void main() { int ch,ch1; char filename[20]; do { clrscr(); printf("\n\t\tFILE OPERATION\n\t\t1.CREATE\n\t\t2.INSERT\n\t\t3.DISPLAY\n\t\t4.SEARCH\n\t\t5.MODIFY\n\t\t6.DELETE\n\t\t7.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch); switch(ch) { case 1: printf("\n\t\tCREATE"); create(); break; case 2: printf("\n\t\tINSERT"); printf("\nENTER FILENAME:"); flushall(); gets(filename); insert(filename); break; case 3: printf("\n\t\tDISPLAY"); display(); break; case 4: printf("\nENTER FILENAME:"); flushall(); gets(filename); do { clrscr(); printf("\n\t\tSEARCH\n\t\t1.BY NAME N ROLL NO\n\t\t2.BY ROLL NO N CLASS\n\t\t3.EXIT"); printf("\n\t\tENTER UR CHOICE"); scanf("%d",&ch1); switch(ch1) { case 1: printf("SEARCH BY NAME N ROLL NO"); searchnr(filename); break; case 2: printf("SEARCH BY CLASS N ROLL NO"); searchcr(filename); break; case 3: break; } }while(ch1!=3); break; case 5: printf("\n\t\tMODIFY"); modify(); break; case 6: printf("\n\t\tDELETE"); delet(); break; case 7: break; } }while(ch!=7); } void create() { FILE *fp; char filename[20]; printf("\nENTER THE FILE NAME:"); flushall(); gets(filename); fp=fopen(filename,"r"); if(fp==NULL) { fclose(fp); fopen(filename,"r"); } else { printf("\nFILE IS ALREADY PRESENT,DO U WANT TO OVERWRITE PRESS 'y'"); if(getche()=='y') { fclose(fp); fopen(filename,"w"); } else { fclose(fp); fopen(filename,"a"); } } fclose(fp); do { insert(filename); printf("DO U WANT TO CONTINUE:y/n"); }while(getche()=='y'); } void insert(char filename[20]) { FILE *fp; char cl[10]; int i=0,r; stud st,st1; fp=fopen(filename,"r"); while(1) { printf("\nENTER ROLL NO:"); scanf("%d",&st.roll); if(st.roll<0&&st.roll>60) printf("\nINVALID ROLL NO"); else break; } while(1) { printf("\nENTER CLASS:fe/se/te/be only"); scanf("%s",st.clas); if(strcmp(st.clas,"fe")!=0&&strcmp(st.clas,"se")!=0&&strcmp(st.clas,"te")!=0&&strcmp(st.clas,"be")!=0) printf("\nINVALID CLASS"); else break; } if(fp==NULL) { printf("\nTHIS IS FIRST ENTRY IN THIS FILE"); } else while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st1.roll,&st1.mark,&st1.add.hno,st1.add.city,st1.name,st1.clas); if(st.roll==st1.roll&&(strcmp(st.clas,st1.clas))==0) { printf("\nALREADY PRESENT"); getch(); i++; break; } } fclose(fp); fp=fopen(filename,"a"); if(i==0) { st=alloc(st); fprintf(fp,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas); } fclose(fp); } void display() { FILE *fp; char filename[20]; int i=0; stud st; printf("\nENTER FILENAME:"); flushall(); gets(filename); fp=fopen(filename,"r"); clrscr(); if(fp==NULL) printf("\nFILE NOT FOUND"); else while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas); printf("\nROLL NO:%d",st.roll); printf("\nMARKS:%d",st.mark); printf("\nHOUSE NO:%d",st.add.hno); printf("\nCITY:%s",st.add.city); printf("\nNAME:%s",st.name); printf("\nCLASS:%s",st.clas); i++; getch(); if(i%2==0) clrscr(); } fclose(fp); } void searchnr(char filename[]) { FILE *fp; char nam[10]; int r,i=0; stud st; fp=fopen(filename,"r"); if(fp==NULL) printf("\nFILE NOT FOUND"); else { printf("\nENTER ROLL NO:"); scanf("%d",&r); printf("\nENTER NAME:"); scanf("%s",nam); while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas); if(st.roll==r&&strcmp(st.name,nam)==0) { i=1; printf("\nROLL NO:%d",st.roll); printf("\nMARKS:%d",st.mark); printf("\nHOUSE NO:%d",st.add.hno); printf("\nCITY:%s",st.add.city); printf("\nNAME:%s",st.name); printf("\nCLASS:%s",st.clas); getch(); } } } if(i==0) printf("\nNOT FOUND"); fclose(fp); getch(); } void searchcr(char filename[20]) { FILE *fp; char cl[10]; int r,i=0; stud st; fp=fopen(filename,"r"); if(fp==NULL) printf("\nFILE NOT FOUND"); else { printf("\nENTER ROLL NO:"); scanf("%d",&r); printf("\nENTER CLASS:"); scanf("%s",cl); while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas); if(st.roll==r&&strcmp(st.clas,cl)==0) { i=1; printf("\nROLL NO:%d",st.roll); printf("\nMARKS:%d",st.mark); printf("\nHOUSE NO:%d",st.add.hno); printf("\nCITY:%s",st.add.city); printf("\nNAME:%s",st.name); printf("\nCLASS:%s",st.name); getch(); break; } } } if(i==0) printf("\nNOT FOUND"); fclose(fp); getch(); } void modify() { FILE *fp,*fp1; char filename[20],filename1[20],cl[10]; int r,i=0,rol,ch; stud st; printf("\nENTER FILENAME:"); flushall(); gets(filename); fp=fopen(filename,"r"); strcpy(filename1,"t"); strcat(filename1,filename); fp1=fopen(filename1,"w"); if(fp==NULL) printf("\nFILE NOT FOUND"); else { printf("\nENTER ROLL NO:"); scanf("%d",&rol); printf("\nCLASS:"); scanf("%s",cl); while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas); if(st.roll==rol&&strcmp(st.clas,cl)==0) { printf("\nOLD DAT IS:\nROLL NO:%d",st.roll); printf("\nMARKS:%d",st.mark); printf("\nHOUSE NO:%d",st.add.hno); printf("\nCITY:%s",st.add.city); printf("\nNAME:%s",st.name); printf("\nCLASS:%s",st.clas); getch(); do { clrscr(); printf("\nWHICH ENTRY U WANT TO MODIFY\n\t\t1.MARKS\n\t\t2.HOUSE NO\n\t\t3.CITY\n\t\t4.NAME\n\t\t5.EXIT"); printf("\nENTER UR CHOICE"); scanf("%d",&ch); switch(ch) { case 1: while(1) { printf("\nENTER NEW MARKS:"); scanf("%d",&st.mark); if(st.mark<0&&st.mark>100) printf("\nINVALID MARKS"); else break; getch(); } break; case 2: while(1) { printf("\nENTER NEW HNO:"); scanf("%d",&st.add.hno); if(st.add.hno<0&&st.add.hno>32000) printf("\nINVALID h.NO"); else break; getch(); } break; case 3: printf("\nENTER CITY:"); scanf("%s",st.add.city); break; case 4: printf("\nENTER NAME:"); scanf("%s",st.name); break; case 5: break; } }while(ch!=5); i=1; fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas); break; } else fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas); } } while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas); fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas); } fclose(fp); fclose(fp1); if(i==0) { printf("\nNOT FOUND"); remove(filename1); } else { remove(filename); rename(filename1,filename); } getch(); } void delet() { FILE *fp,*fp1; char filename[20],cl[10],filename1[20]; int r,i=0; stud st; printf("\nENTER FILENAME:"); flushall(); gets(filename); fp=fopen(filename,"r"); strcpy(filename1,"t"); strcat(filename1,filename); fp1=fopen(filename1,"w"); if(fp==NULL) printf("\nFILE NOT FOUND"); else { printf("\nENTER ROLL NO:"); scanf("%d",&r); printf("\nENTER CLASS:"); scanf("%s",cl); while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas); if(st.roll==r&&strcmp(st.clas,cl)==0) { i=1; break; } else fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas); } } while(!feof(fp)) { fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas); fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas); } fclose(fp); fclose(fp1); if(i==0) { printf("\nNOT FOUND"); remove("filename1"); } else { remove(filename); rename(filename1,filename); printf("DATA IS DELETED"); } getch(); } stud alloc(stud st) { while(1) { printf("\nENTER MARKS:"); scanf("%d",&st.mark); if(st.mark<0&&st.mark>100) printf("\nINVALID ROLL NO"); else break; } while(1) { printf("\nENTER HNO:"); scanf("%d",&st.add.hno); if(st.add.hno<0&&st.add.hno>32000) printf("\nINVALID ROLL NO"); else break; } printf("\nENTER CITY:"); scanf("%s",st.add.city); printf("\nENTER NAME:"); scanf("%s",st.name); getch(); return st; }
Leave a Reply