i am not allowed to use data structures
i am allowed to have only the strings of one ow from each file
Printable View
i am not allowed to use data structures
i am allowed to have only the strings of one ow from each file
i got a problem in your algorithm
its copying 005 many times
instead of once
each time we update the smallest it
puts in the file
005 003 005 >>puts 005 in the file
005 004 005 >>puts 005 in the file
005 005 005 >>puts 005 in the file
and my while condition doesnt work
its not stoping in that point why??
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("c:\\1.txt","r");
file[1]=fopen("c:\\2.txt","r");
file[2]=fopen("c:\\3.txt","r");
common=fopen("c:\\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);
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;
}