Hi,
I have to find the ‘position’ of ‘23’ in file a1.txt. At the corresponding ‘position’
in a2.txt, I want to add 500 to member value. I am able to pick up the values from both a1.txt and a2.txt but the problem comes when I a trying to replace the value in a2.txt by adding 500 to the corresponding member value. The value is not getting printed in a2.txt.
I am using gcc compiler. Kindly help..
a1.txt:
11
12
13
....
23
24
25
a2.txt:
61
62
63
...
73
74
75
Code:
#include<stdio.h>
int main()
{
FILE *fp,*fp1;
int i,ch;
long n;
fp1=fopen("a1.txt","r");
while(fscanf(fp1,"%d",&i) == 1)
{
if(i==23)
{
n=ftell(fp1);
printf("%ld\n",n);
break;
}
}
fclose(fp1);
n=n-2; //offset starts with '0'
fp=fopen("a2.txt","rw");
fseek(fp,n,0);
fscanf(fp,"%d",&i);
fprintf(fp,"%d\n",i+5);
fclose(fp);
return 0;
}