why this while condition doesnt stop the loop??
i told it that when it reaches EOF then stop but it keeps putting 005 over and over ...
why ??
it should get out of the loop after it wrote 005
???
file1
000
001
002
003
005
file2
003
004
005
file3
005
006
007
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define false 0
#define true 1
int equal(char*f1,char*f2,char*f3);
int smallest(char*f1,char*f2,char*f3);
int main()
{
FILE *file[3];
FILE *common;
char f[3][20];
int small;
int equall;
file[0]=fopen("g:\\1.txt","r");
file[1]=fopen("g:\\2.txt","r");
file[2]=fopen("g:\\3.txt","r");
common=fopen("g:\\common.txt","w");
fscanf(file[0],"%9s",f[0]);
fscanf(file[1],"%9s",f[1]);
fscanf(file[2],"%9s",f[2]);
while(fgetc(file[0])!=EOF||fgetc(file[1])!=EOF||fgetc(file[2])!=EOF)
{
small=smallest(f[0],f[1],f[2]);
equall=equal(f[0],f[1],f[2]);
if(equall!=-1)
{
fputs(f[equall],common);
fscanf(file[equall],"%20s",f[equall]);
fputc('\n',common);
}
fscanf(file[small],"%20s",f[small]);
}
fclose(file[1]);
fclose(file[2]);
fclose(file[0]);
return 0;
}
int smallest(char* f_a,char* f_b,char* f_c)
{
int i=0;
char* pLowest = f_a;
if(strcmp(pLowest, f_b) > 0)
{
i=1;
pLowest = f_b;
}
if(strcmp(pLowest, f_c) > 0)
{
pLowest = f_c;
i=2;
}
return i;
}
int equal(char* f1,char*f2,char*f3)
{
if(strcmp(f1,f3)==0)
{
return 0;
}
if(strcmp(f1,f2)==0)
{
return 1;
}
if(strcmp(f2,f3)==0)
{
return 2;
}
return -1;
}