I am reading a file named "text.txt" that contains a series of numbers with a space in between. I want to write in a new file named "text_nr.txt", all the numbers in "text.txt" but without repetition! My code runs in an infinite loop at the second while loop, but I can't understand why.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n=0,m=0,nr;
FILE *fp1;
FILE *fp2;
fp1 = fopen("text.txt","r");
fp2 = fopen("text_nr.txt","a+");
while(fscanf(fp1,"%d",&n) != EOF){
nr=0;
while(fscanf(fp2,"%d",&m) != EOF){
printf("%d %d ",m,n);
if(n != m) continue;
else{
nr++;
break;
}
}
if(nr == 0){
fprintf(fp2,"%d ",n);
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
scanfis complicated, and you will learn more if you usefreadandstrtolinstead.