I'm trying to arrange some statements in an alphabetical order
(this is just a prototype to illustrate the problem I'm facing with my actual program)
so the program should read the statements from a text file , arrange, then print them back on the same text file(overwrite)
problem :
the pointers seem to read the non-updated lines rather than reading the ones updated by the previous execution of the for loop ..
here's the code :
insert
Code:
#include<stdio.h>
#include<string>
int main()
{
FILE *fp1,*fp2,*fp3 ;
char name[10],name1[10] ;
int i,ID,ID1 ;
fp1 = fopen("test.txt","r+") ;
fp2 = fopen("test.txt","r+") ;
fp3 = fopen("test.txt","r+") ;
fseek(fp2,9,SEEK_SET) ;
for(i=0;i<3;i++)
{
fscanf(fp1,"%s\t%d",&name,&ID) ;
fscanf(fp2,"%s\t%d",&name1,&ID1) ;
if(name[0] < name1[0])
{ fprintf(fp3,"%s\t%d\n%s\t%d\n",name,ID,name1,ID1) ;}
if(name1[0] < name[0])
{fprintf(fp3,"%s\t%d\n%s\t%d\n",name1,ID1,name,ID) ;}
}
fclose(fp1) ;
fclose(fp2) ;
fclose(fp3) ;
return(0) ;
}
the file before the code execution:
ccccc 3
bbbbb 2 -------------------> (read twice)
fffff 6
eeeee 5
aaaaa 1
ddddd 4
the file after executon:
bbbbb 2
ccccc 3 ----> should've been read
bbbbb 2
fffff 6
eeeee 5
fffff 6
note : this code will be executed more than once using additional for loops to ensure full arrangement.
any ideas are welcomed .. your help would be appreciated .