Hi guys i have a project due to friday where everything works except this one funktion. i have a txt file in which i wanted to read until an ID of my struct comes up. it should then scan my new entrys for this struct and write it over the old struct. Has anyone got an idea why this code isnt working properly? It just writes in other structures and leaves the one i wanted it to write in untouched.
Thats the struct and the code of function:
struct Medicine{
int quantity;
float price;
char ExpDate[15];
char Company[20];
char Supplier[30];
char MediName[20];
char info[1000];
char MediID [30];
void editmed(){
FILE *pt;
int flag=0;
struct Medicine m,mt;
char ID[30];
pt=fopen("mlist.txt","rb+");
if(pt==NULL){
printf("file opening error");
getch();
medicine();
}
else{
system("cls");
printf("\nEnter Medicine ID to modify:\n");
scanf("%s",ID);
fflush(stdin);
while(fread(&m,sizeof(struct Medicine),1,pt)==1){
if(strcmp(ID,m.MediID)==0){
printf("Enter Medicine ID (only numbers):\n");
gets(mt.MediID);
fflush(stdin);
printf("Enter Medicine Name:\n");
gets(mt.MediName);
fflush(stdin);
printf("Enter Quantity:\n");
scanf("%d",&mt.quantity);
fflush(stdin);
printf("Enter Price:\n");
scanf("%f",&mt.price);
fflush(stdin);
printf("Enter Expiry Date(dd.mm.yyyy):\n");
gets(mt.ExpDate);
fflush(stdin);
printf("Enter Company:\n");
gets(mt.Company);
fflush(stdin);
printf("Enter Supplier:\n");
gets(mt.Supplier);
fflush(stdin);
printf("Enter Info about medicine:\n");
gets(mt.info);
fflush(stdin);
fseek(pt,sizeof(m),SEEK_CUR);
fwrite(&mt,sizeof(struct Medicine),1,pt);
flag=1;
//aus Schleife aussteigen wenn gesuchter Eintrag gefunden wurde
break;
}
fflush(stdin);
}
if(flag==1){
printf("\n your data has been modified");
}
else{
printf(" \n data is not found");
}
fclose(pt);
}
printf("\n Enter any key to go back to medicine menu\n");
getch();
fflush(stdin);
medicine();