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);
}
Help with fseek
Started by hbk, Mar 10 2010 06:13 PM
11 replies to this topic
#1
Posted 10 March 2010 - 06:13 PM
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.
|
|
|
#2
Posted 11 March 2010 - 12:52 AM
fseek(customer1,-0L,SEEK_CUR);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.
I also found a few other things wrong with your code:
if((customer1=fopen("customer1.txt","r+"))==NULL)
printf("THE FILE IS EMPTY");
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.fflush(stdin);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.)
gets(info.Name);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.
If you have any questions, just ask. :)
sudo rm -rf /
#3
Posted 11 March 2010 - 05:17 AM
How would you recommend i correct this code to search and update.
#4
Posted 11 March 2010 - 01:09 PM
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 /
#5
Posted 11 March 2010 - 03:49 PM
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
#6
Posted 11 March 2010 - 05:38 PM
Can you post your code? I thin I have an idea but it depends on your code.
sudo rm -rf /
#7
Posted 11 March 2010 - 05:50 PM
Hers it is or you want the entire program 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);
}
#8
Posted 11 March 2010 - 05:56 PM
#9
Posted 11 March 2010 - 06:06 PM
i was just experimenting
#10
Posted 11 March 2010 - 06:16 PM
That's your problem. You completely ignored what I said.
void updaterecord(void)
{
system("color 2");
char choice4;
char Target[50];
int Found=0;
int tell=0;
[COLOR=RED]long int offset = -1;[/COLOR]
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)
{
[COLOR=RED]offset = ftell(customer1);[/COLOR]
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...
[COLOR=RED]
fseek(customer1,offset,SEEK_SET);
[/COLOR]
...blah...
}
else
if(!Found)
printf("\n\n\t\t THERES NO SUCH RECORD");
}
fclose(customer1);
}
sudo rm -rf /
#11
Posted 11 March 2010 - 06:25 PM
Ratings all the way my friend,it worked.thanks a whole lot.how can i add to your rep
#12
Posted 11 March 2010 - 06:28 PM
Little star button on the bottom of each post near "Blog this Post." Thanks! Glad I could help. :)
sudo rm -rf /


Sign In
Create Account


Back to top









