-
after the pointed comment
i dont know from what file the smallest belongs to
i dont know in what two files i got the equality
so i could write it somewhere and go the the next line of the "smallest" file
??
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define false 0
#define true 1
char* equal(char*f1,char*f2,char*f3);
char* smallest(char*f1,char*f2,char*f3);
int main()
{
FILE *file1,*file2,*file3;
char f1[20];
char f2[20];
char f3[20];
char* small;
char* equall;
file1=fopen("c:\\1.txt","r");
file2=fopen("c:\\2.txt","r");
file3=fopen("c:\\3.txt","r");
fscanf(file1,"%9s",f1);
fscanf(file2,"%9s",f2);
fscanf(file3,"%9s",f3);
small=smallest(f1,f2,f3);
equall=equal(f1,f2,f3); //how to continue after this point
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}
char* smallest(char*f1,char*f2,char*f3)
{
char* pLowest = f1;
if(strcmp(pLowest, f2) > 0)
pLowest = f2;
if(strcmp(pLowest, f3) > 0)
pLowest = f3;
return pLowest;
}
char* equal(char*f1,char*f2,char*f3)
{
if(strcmp(f1,f3)==0)
{
return f1;
}
if(strcmp(f2,f3)==0)
{
return f2;
}
return NULL;
}
-
So, you need a function that actually tells you which file to read next, when you have three sets of SSN.
And of course, you need to do a loop of some sort.
--
Mats
-
how to return the file in this function
how to know what file is the smallest string
??
Code:
char* smallest_file(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
{
char* pLowest = f1;
if(strcmp(pLowest, f2) > 0)
pLowest = f2;
if(strcmp(pLowest, f3) > 0)
pLowest = f3;
return pLowest;
}
-
is there any shorter way then
Code:
char* smallest_file(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
{
char* pLowest = f1;
if(strcmp(pLowest, f2) > 0)
pLowest = f2;
if(strcmp(pLowest, f3) > 0)
pLowest = f3;
if(strcmp(f1,pLowest)==0)
{
return file1;
}
if(strcmp(f2,pLowest)==0)
{
return file2;
}
if(strcmp(f3,pLowest)==0)
{
return file3;
}
}
-
Well, I suppose you could write a function that updates the lowest string(s) by reading from the relevant file, rather than making a function that returns the file wanted.
--
Mats
-
you say to write a function that updates the lowest string(s) by reading from the relevant file.
but i have to know from what file the lowest string came from.
so i cant understand how i can manage doing what you told me
without building such function?
-
If you keep the files and the strings in the same order at all times, you can determine if the value from file X needs updating, and read the file.
--
Mats
-
you say
build some how that you could know which file to update each time
but its too abstract
what sub operations i need to build?
-
Shouldn't need many sub-operations. Just string compare and fscanf() or something like that, and a few if-statements.
I just hacked one up in about 13 lines of code, with 5 if-statements (including finding the lowest one).
--
Mats
-
ok here is the comparing part
Code:
char* smallest(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
{
char* pLowest = f1;
if(strcmp(pLowest, f2) > 0)
pLowest = f2;
if(strcmp(pLowest, f3) > 0)
pLowest = f3;
return pLowest;
}
here is the scanf part
Code:
fscanf(file1,"%9s",f1);
fscanf(file2,"%9s",f2);
fscanf(file3,"%9s",f3);
for what reason i need to use
"a few if-statements"
-
You need if-statments to determine if you need to read from the file or not (if you already have a value higher than the lowest, you should not read the file).
--
Mats
-
ok i tried to write it
but how to know which string is the smallest and which file it belongs to
without writing the previous function that i showed
??
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define false 0
#define true 1
char* equal(char*f1,char*f2,char*f3);
char* smallest(char*f1,char*f2,char*f3);
int main()
{
FILE *file1,*file2,*file3;
char f1[20];
char f2[20];
char f3[20];
char* small;
char* equall;
file1=fopen("c:\\1.txt","r");
file2=fopen("c:\\2.txt","r");
file3=fopen("c:\\3.txt","r");
while(fgetc(f1)!=EOF)||(fgetc(f2)!=EOF)||(fgetc(f1)!=EOF))
{
fscanf(file1,"%9s",f1);
fscanf(file2,"%9s",f2);
fscanf(file3,"%9s",f3);
small=smallest(f1,f2,f3);
equall=equal(f1,f2,f3);
}
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}
char* smallest(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
{
char* pLowest = f1;
if(strcmp(pLowest, f2) > 0)
pLowest = f2;
if(strcmp(pLowest, f3) > 0)
pLowest = f3;
return pLowest;
}
char* equal(char*f1,char*f2,char*f3)
{
if(strcmp(f1,f3)==0)
{
return f1;
}
if(strcmp(f2,f3)==0)
{
return f2;
}
return NULL;
}
-
-
how to combine those parts
-
How about using a dynamically allocated array of structs whose elements are: SSN, current file number and a count. SSNs whose count equals 3 are present in all of the files. Just my 2c.