// Direct Access Files // Gaurang Sinha // gaurang85@rediffmail.com // gaurang.cjb.net -or- gaurang85.tripod.com #include #include #include #include struct node{ char name[20]; int roll,marks,age; long link; }db; long val(int no),prev(long pos); void add(),disp(),disp_all(),del(); FILE *fp; int main() { int s; fp=fopen("daf.dat","rb"); if(!fp) //Check If file exists { fp=fopen("daf.dat","wb+"); //If Not create file strcpy(db.name," "); db.marks = db.age = db.roll = db.link = -1; for(s=0;s<10;s++) { fseek(fp,s*sizeof(struct node),SEEK_SET); fwrite(&db,sizeof(struct node),1,fp); } fclose(fp); } A: clrscr(); printf( "\n\t\tMENU" "\n\t1. Add Record" "\n\t2. Delete Record" "\n\t3. Search Record" "\n\t4. Display All Records" "\n\t5. Exit" "\n\t::>"); scanf("%d",&s); if(s==5)return 0; switch(s) { case 1: add(); goto A; case 2: del(); goto A; case 3: disp(); goto A; case 4: disp_all(); goto A; default: goto A; } } long val(int no) //Hash Function With Offset ! { return ((no%10)*sizeof(struct node)); } void add() //Add's Record Using Channing Without Replacement { struct node test; long offset,link; printf("\n\n\t\tEnter Following Data"); printf("\n\tName : "); scanf("%s",db.name); printf("\n\tRoll : "); scanf("%d",&db.roll); printf("\n\tMarks : "); scanf("%d",&db.marks); printf("\n\tAge : "); scanf("%d",&db.age); offset=val(db.roll); fp=fopen("daf.dat","rb+"); //Open For Reading & Writing In Binary Mode if(!fp){printf("\n\nCould Not Open DataBase File !!!");getch();return;} fseek(fp,offset,SEEK_SET); fread(&test,sizeof(struct node),1,fp); if(test.roll==-1) //If Current Position Is Empty { fseek(fp,offset,SEEK_SET); fwrite(&db,sizeof(struct node),1,fp); } else { while(test.link!=-1) //Go To End Of Chain { offset=test.link; fseek(fp,offset,SEEK_SET); fread(&test,sizeof(struct node),1,fp); } while(test.roll!=-1) //Find Next Free Position After End Of Chain { link=ftell(fp); fread(&test,sizeof(struct node),1,fp); } fseek(fp,link,SEEK_SET); fwrite(&db,sizeof(struct node),1,fp); fseek(fp,offset,SEEK_SET); fread(&test,sizeof(struct node),1,fp); test.link=link; fseek(fp,offset,SEEK_SET); fwrite(&test,sizeof(struct node),1,fp); } fseek(fp,0,SEEK_END); strcpy(db.name," "); db.marks = db.age = db.roll = db.link = -1; fwrite(&db,sizeof(struct node),1,fp); fclose(fp); } void disp() { int roll; long offset; printf("\n\n\t\tEnter Roll No : "); scanf("%d",&roll); offset=val(roll); fp=fopen("daf.dat","rb+"); //Open For Reading & Writing In Binary Mode if(!fp){printf("\n\nCould Not Open DataBase File !!!");getch();return;} fseek(fp,offset,SEEK_SET); while((db.roll!=roll)&&(!feof(fp))) fread(&db,sizeof(struct node),1,fp); if(db.roll!=-1) { printf("\n\t\tStudents Infomation"); printf("\n\n\tName : %s",db.name); printf("\n\tRoll No : %d",db.roll); printf("\n\tAge : %d",db.age); printf("\n\tMarks : %d",db.marks); } else printf("\nNo Such Record Found !!!"); getch(); fclose(fp); } void disp_all() { long pos; printf("\n\nDisplaing All Data In Database File"); printf("\n\n\tPos\tRoll\tName\t\tMarks\tAge\tLink"); fp=fopen("daf.dat","rb+"); //Open For Reading & Writing In Binary Mode if(!fp){printf("\n\nCould Not Open DataBase File !!!");getch();return;} while(!feof(fp)) { pos=ftell(fp); fread(&db,sizeof(struct node),1,fp); if(db.roll!=-1) printf("\n\t%ld\t %d\t%s\t\t %d\t %d\t%ld", pos,db.roll,db.name,db.marks,db.age,db.link); } getch(); fclose(fp); } void del() { long offset; int roll,flag=0; printf("\n\n\tEnter Roll No To Delete : "); scanf("%d",&roll); offset=val(roll); fp=fopen("daf.dat","rb+"); //Open For Reading & Writing In Binary Mode if(!fp){printf("\n\nCould Not Open DataBase File !!!");getch();return;} fseek(fp,offset,SEEK_SET); fread(&db,sizeof(struct node),1,fp); if(db.roll!=roll) { while((db.roll!=roll)&&(db.link!=-1)) { fseek(fp,db.link,SEEK_SET); fread(&db,sizeof(struct node),1,fp); } if(db.roll!=roll) { printf("\nNo Such Record !!! Cannot Delete !!!"); return; } else { long last; struct node temp; offset = (ftell(fp)-sizeof(struct node)); last=prev(offset); if(last!=-1) { fseek(fp,last,SEEK_SET); fread(&temp,sizeof(struct node),1,fp); fseek(fp,offset,SEEK_SET); fread(&db,sizeof(struct node),1,fp); temp.link=db.link; fseek(fp,last,SEEK_SET); fwrite(&temp,sizeof(struct node),1,fp); } strcpy(db.name," "); db.marks = db.age = db.roll = db.link = -1; fseek(fp,offset,SEEK_SET); fwrite(&db,sizeof(struct node),1,fp); printf("\n\n\tRecord Deleted Successfully !!!"); } } else { strcpy(db.name," "); db.marks = db.age = db.roll = db.link = -1; fseek(fp,offset,SEEK_SET); fwrite(&db,sizeof(struct node),1,fp); printf("\n\n\tRecord Deleted Successfully !!!"); } fclose(fp); getch(); } long prev(long pos) { long offset; fp=fopen("daf.dat","rb+"); //Open For Reading & Writing In Binary Mode if(!fp){printf("\n\nCould Not Open DataBase File !!!");getch();return;} while(!feof(fp)) { offset=ftell(fp); fread(&db,sizeof(struct node),1,fp); if(db.link==pos) return offset; } return (-1); }