#include #include struct Kayit { int ogrenciNo; int dersKodu; int puan; }; struct Kayit data; void kayitekle() { FILE *openfile; openfile = fopen("ogrencidata.bin","ab+"); if(openfile == NULL) { printf("Error in Opening file\nMake sure your file is not write protected","Warning"); } else { fflush(stdin); printf("\t\tOgrenci No: \n"); scanf("%d",&data.ogrenciNo); printf("\t\tDers Kodu: \n"); scanf("%d",&data.dersKodu); printf("\t\tPuan: \n"); scanf("%d",&data.puan); fwrite(&data, sizeof(data), 1, openfile); } fclose(openfile); return; } void kayitbul() { int ogrenciNumarasi; int isFound = 0; printf("Ogrenci Numarasini Giriniz: \n"); fflush(stdin); scanf("%d",&ogrenciNumarasi); FILE *openfile; openfile = fopen("ogrencidata.bin","rb"); while(fread(&data,sizeof(data),1,openfile) == 1) { if(ogrenciNumarasi == data.ogrenciNo) { isFound = 1; break; } } if(isFound == 1) { printf("\t\tOgrenci bulundu\n"); printf("\t\tOgrenci no: %d \n",data.ogrenciNo); printf("\t\tDers Kodu: %d \n",data.dersKodu); printf("\t\tPuan: %d \n",data.puan); } else { printf("Ogrenci bulunamadi"); } fclose(openfile); return; } void kayitlarilistele() { FILE *openfile; int i; openfile = fopen("ogrencidata.bin","rb"); printf("\nTum ogrenciler:"); printf("\n======================="); i=1; while((fread(&data,sizeof(data),1,openfile) == 1)) { printf("\nOgrenci sirasi:%d",i); printf("\n------------------"); printf("\nOgrenci numarasi:%d",data.ogrenciNo); i++; printf("\n======================="); } fclose(openfile); } void kayitguncelle() { printf("Kayit guncelleme\n"); int ogrenciNumarasi; int isFound = 0; printf("Kayit guncellemek istediginiz ogrencinin numarasini giriniz: "); fflush(stdin); scanf("%d",&ogrenciNumarasi); FILE *openfile; openfile = fopen("ogrencidata.bin","rb+"); while(fread(&data, sizeof(data),1,openfile) == 1) { if(ogrenciNumarasi == data.ogrenciNo) { fflush(stdin); printf("Ogrenci Numarasi: "); scanf("%d",&data.ogrenciNo); printf("Ders Kodu: "); scanf("%d",&data.dersKodu); printf("Puan: "); scanf("%d",&data.puan); fseek(openfile,-sizeof(data),SEEK_CUR); fwrite(&data,sizeof(data),1,openfile); printf("Ogrenci basariyla guncellendi."); isFound = 1; break; } } if(!isFound) { printf("Kayit bulunamadi"); } fclose(openfile); return; } void kayitsil() { printf("Kayit silme ekrani\n"); int ogrenciNumarasi; int isFound = 0; printf("Silmek istediginiz ogrencinin numarasini giriniz: "); fflush(stdin); scanf("%d",&ogrenciNumarasi); FILE *openfile, *temporary; openfile = fopen("ogrencidata.bin","rb"); temporary = fopen("temp.bin", "wb"); while(fread(&data,sizeof(data),1,openfile) == 1) { if(ogrenciNumarasi == data.ogrenciNo) { printf("Silindi"); isFound = 1; } else { fwrite(&data,sizeof(data),1,temporary); } } if(!isFound) { printf("istenilen ogrenci bulunamadi"); } fclose(openfile); fclose(temporary); remove("ogrencidata.bin"); rename("temp.bin","ogrencidata.bin"); printf("\nThe record is sucessfully deleted"); return; } void showIndex(struct Kayit *index) { printf("\t\tOgrenci No : %d\t\t\t\t\t\tDers Kodu : %d\n\n", index->ogrenciNo,index->dersKodu); } void kayitlarisiralilistele(){ FILE *openfile; if ((openfile = fopen("ogrencidata.bin", "rb"))!=NULL) { size_t itemSize = sizeof(data); struct Kayit item; fread(&item,1,itemSize, openfile); while (!feof(openfile)) { showIndex(&item); fread(&item,1,itemSize, openfile); } fclose(openfile); } } void kayitlarisirala() { FILE *openfile; if ((openfile = fopen("ogrencidata.bin", "rb+"))!=NULL) { size_t itemSize = sizeof(data); struct Kayit data1, data2; int flag = 1; // bubble sort while(flag) { flag = 0; fread(&data1, itemSize, 1, openfile); fread(&data2, itemSize, 1, openfile); while (!feof(openfile)) { if (data2.ogrenciNo < data1.ogrenciNo) { fseek(openfile, (itemSize * -2), SEEK_CUR); fwrite(&data2, itemSize, 1, openfile); fwrite(&data1, itemSize, 1, openfile); flag = 1; } else { data1 = data2; } fread(&data2, itemSize, 1, openfile); } if (flag) { rewind(openfile); } } fclose(openfile); } kayitlarisiralilistele(); } void anaekran() { int option; while(1) { printf("\t\tChoose 1. Add Student\n"); printf("\t\tChoose 2. Search Student\n"); printf("\t\tChoose 3. Update Student\n"); printf("\t\tChoose 4. Delete Student\n"); printf("\t\tChoose 5. Show all Students\n"); printf("\t\tChoose 6. Show all Students with Sorting\n"); printf("\t\tChoose 7. Exit\n"); scanf("%d",&option); switch(option) { case 1: kayitekle(); break; case 2: kayitbul(); break; case 3: kayitguncelle(); break; case 4: kayitsil(); break; case 5: kayitlarilistele(); break; case 6: kayitlarisirala(); break; case 7: exit(0); break; default: break; } } } int main() { anaekran(); return 0; }