Hey im new to this forum and i have a piece of code in which i use fseek to try and update a particular record. It does work but when i seek the record its writing the new data at the end of the line of the data i want to overwrite.Any help is appreciated.
Code:void updaterecord(void) { system("color 2"); char choice4; char Target[50]; int Found=0; if((customer1=fopen("customer1.txt","r+"))==NULL) printf("THE FILE IS EMPTY"); else { printf("\n\n\t\t ENTER ID NUM:"); scanf("%s",&Target); while(!feof(customer1)&& Found==0) { fscanf(customer1,"%s %s %s %s %s %s %s %s",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app); if(strcmp(Target,info.regis)==0) Found=1; } if(Found) { printf("\n\t\t REGISTRATION:%s\n",info.regis); printf("\n\t\t NAME:%s\n",info.Name); printf("\n\t\t ADDRESS:%s\n",info.address); printf("\n\t\t NUMBER:%s\n",info.number); printf("\n\t\t DATE OF BIRTH:%s\n",info.dob); printf("\n\t\t LAST TREATMENT:%s\n",info.treatment); printf("\n\t\t ALLERGIES:%s\n",info.allergies); printf("\n\t\t LAST APPOINTMENT:%s\n",info.app); printf("\n\n\t\t ENTER THE NEW DATA BELOW:"); printf("\n\n\t\t\t NAME: "); fflush(stdin); gets(info.Name); printf("\n\n\t\t\t ADDRESS: "); gets(info.address); printf("\n\n\t\t\t NUMBER: "); gets(info.number); printf("\n\n\t\t\t TREATMENT: "); gets(info.treatment); printf("\n\n\t\t\t ALLERGIES: "); gets(info.allergies); printf("\n\n\t\t\t DATE OF BIRTH 12/12/12: "); gets(info.dob); printf("\n\n\t\t\t DATE OF LAST APPOINTMENT 12/12/12: "); gets(info.app); fseek(customer1,-0L,SEEK_CUR); fprintf(customer1,"%s %s %s %s %s %s %s %s ",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app); printf("\n\n\t\t THE DETAILS OF THE ITEM HAS BEEN UPDATED SUCCESSFULLY"); rewind(customer1); } else if(!Found) printf("\n\n\t\t THERES NO SUCH RECORD"); } fclose(customer1); }
This doesn't go to the beginning of the line. It actually doesn't do anything. You're telling it to move zero bytes from the current position which is...the current position. You're going to have to figure out some other way to keep track of the beginning of the line.Code:fseek(customer1,-0L,SEEK_CUR);
I also found a few other things wrong with your code:
Not true. If you get NULL back that means that there was an error opening the file, most likely that it doesn't exist. This is different than an empty file, which exists but doesn't have anything in it.Code:if((customer1=fopen("customer1.txt","r+"))==NULL) printf("THE FILE IS EMPTY");
Do not do this. It's undefined behavior by the ANSI C standard, which means it won't work everywhere. Some compilers let you get away with it, others won't and you're not going to get the expected results. Figure out some other way, like looping through fgetc until you hit a newline. (I'm sure there's a better way but I can't remember what right now.)Code:fflush(stdin);
Don't ever use gets(). You can easily get a buffer overrun and blow away your own code, or hackers could exploit this and overwrite your code with their own, if they want. (This is how Internet Explorer used to get hacked so easily. It had a lot of these.) Use fgets(info.Name, length_of_Name, stdin) instead. Much safer.Code:gets(info.Name);
If you have any questions, just ask.![]()
sudo rm -rf /
How would you recommend i correct this code to search and update.
Before your fscanf line, use ftell() to remember the file offset at the beginning of the line. When you find the record, call fseek() using that file offset.
sudo rm -rf /
That still not working,could you give me a code example or is its because im writing to a txt file and i heard that fseek and txt file don't get along very well
Can you post your code? I thin I have an idea but it depends on your code.
sudo rm -rf /
Hers it is or you want the entire program code
Code:void updaterecord(void) { system("color 2"); char choice4; char Target[50]; int Found=0; int tell=0; if((customer1=fopen("customer1.txt","r+"))==NULL) printf("THE FILE IS EMPTY"); else { printf("\n\n\t\t ENTER ID NUM:"); scanf("%s",&Target); while(!feof(customer1)&& Found==0) { fscanf(customer1,"%s %s %s %s %s %s %s %s",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app); if(strcmp(Target,info.regis)==0) Found=1; } if(Found) { printf("\n\t\t REGISTRATION:%s\n",info.regis); printf("\n\t\t NAME:%s\n",info.Name); printf("\n\t\t ADDRESS:%s\n",info.address); printf("\n\t\t NUMBER:%s\n",info.number); printf("\n\t\t DATE OF BIRTH:%s\n",info.dob); printf("\n\t\t LAST TREATMENT:%s\n",info.treatment); printf("\n\t\t ALLERGIES:%s\n",info.allergies); printf("\n\t\t LAST APPOINTMENT:%s\n",info.app); printf("\n\n\t\t ENTER THE NEW DATA BELOW:"); printf("\n\n\t\t\t NAME: "); fflush(stdin); gets(info.Name); printf("\n\n\t\t\t ADDRESS: "); gets(info.address); printf("\n\n\t\t\t NUMBER: "); gets(info.number); printf("\n\n\t\t\t TREATMENT: "); gets(info.treatment); printf("\n\n\t\t\t ALLERGIES: "); gets(info.allergies); printf("\n\n\t\t\t DATE OF BIRTH 12/12/12: "); gets(info.dob); printf("\n\n\t\t\t DATE OF LAST APPOINTMENT 12/12/12: "); gets(info.app); fseek(customer1,-80L,SEEK_CUR); fprintf(customer1,"%s %s %s %s %s %s %s %s ",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app); printf("\n\n\t\t THE DETAILS OF THE ITEM HAS BEEN UPDATED SUCCESSFULLY"); rewind(customer1); } else if(!Found) printf("\n\n\t\t THERES NO SUCH RECORD"); } fclose(customer1); }
Is every entry exactly 80 characters long? It doesn't look like it.
sudo rm -rf /
i was just experimenting
That's your problem. You completely ignored what I said.
Code:void updaterecord(void) { system("color 2"); char choice4; char Target[50]; int Found=0; int tell=0; long int offset = -1; if((customer1=fopen("customer1.txt","r+"))==NULL) printf("THE FILE IS EMPTY"); else { printf("\n\n\t\t ENTER ID NUM:"); scanf("%s",&Target); while(!feof(customer1)&& Found==0) { offset = ftell(customer1); fscanf(customer1,"%s %s %s %s %s %s %s %s",info.regis,info.Name,info.address,info.number,info.dob,info.treatment,info.allergies,info.app); if(strcmp(Target,info.regis)==0) Found=1; } if(Found) { ...blah... fseek(customer1,offset,SEEK_SET); ...blah... } else if(!Found) printf("\n\n\t\t THERES NO SUCH RECORD"); } fclose(customer1); }
sudo rm -rf /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks