another problem is that when i check if its EOF
i skip one char forward because every command on a file is self progressing
how to check if it reached EOF without going one file forward
another problem is that when i check if its EOF
i skip one char forward because every command on a file is self progressing
how to check if it reached EOF without going one file forward
That is true - you should not use fgetc() to track if the file has ended or not. You can probably get away with using feof() in this case - just make sure you test every fscanf() to see if it's EOF.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
what to do in the equal function if it finds "99999" in one of the strings?
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
you said
"so it's only if TWO strings are "9999" that you need to worry"
how to deal with a case of two 9999?
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Can I also say: You really need to THINK about the problem, not just post another post on this forum when you don't understand something. We can't hold your hand forever.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
the problem is that its not a school project
its a question from a test
i have about 30 minutes for it . and 2 pages of A4 size .
so what you are suggesting
its too long
is there any shorter way to handle this EOF stuf??
Well, you can probably come up with a completely different solution. But chances are that the new test won't have this PRECISE question, but something slightly different, and it is the ability to solve problems that you need to show, not the ability to ask questions for several days on a forum. Most lecturers and such will accept a bit of leeway on the technical side of things as long as the overall solution is sound.
Also, if it looks like you are getting to much more than 2 A4 pages, perhaps you are going down the wrong route altogether.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
now i get only one 005
??
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); } if(getc(file[small])!=EOF) { fscanf(file[small],"%20s",f[small]); } else { strcpy(f[small],"999999"); } } fclose(file[1]); fclose(file[2]); fclose(file[0]); fclose(common); 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)&&(strcmp(f1,"999999")!=0)) { return 0; } if((strcmp(f1,f2)==0)&&(strcmp(f1,"999999")!=0)) { return 1; } if((strcmp(f2,f3)==0)&&(strcmp(f3,"999999")!=0)) { return 2; } return -1; }
Right, I should perhaps not be so cocky - I know the problem, and I just spent the better part of half an hour, and still couldn't do it. I
I've written a bit of code that looks about right, but it's no working. And it's 95 lines.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
This is my submission. It is not written to be an example of how to write great code. It lacks proper error-checking for example, and variable names aren't descriptive at all.
But in an effort to try to see if I could write it in half an hour, I took some short-cuts (and I failed, but I only spent a few minutes with the debugger to get it working).
--Code:#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> void readOneStr(FILE *f, char **s) { char temp[100]; printf("readOneStr(%p)\n", *s); if (!*s || !feof(f)) { if (fgets(temp, sizeof(temp), f)) { temp[strlen(temp)-1] = 0; *s = realloc(*s, strlen(temp)+1); // if *s == NULL error! strcpy(*s, temp); } else { free(*s); *s = NULL; } } } char *findSmallest(char **s1, char **s2, char **s3) { char *smallest = *s1; printf("Finding smallest\n"); if (!smallest || (*s2 && (strcmp(*s2, smallest) < 0))) { printf("Finding smallest\n"); smallest = *s2; } printf("Finding smallest\n"); if (!smallest || (*s3 && (strcmp(*s3, smallest) < 0))) { printf("Finding smallest\n"); smallest = *s3; } return smallest; } void readStrs(FILE *f1, FILE *f2, FILE *f3, char **s1, char **s2, char **s3) { char *smallest = findSmallest(s1, s2, s3); printf("readStr\n"); if (!*s1 || !strcmp(*s1, smallest)) readOneStr(f1, s1); if (!*s2 || !strcmp(*s2, smallest)) readOneStr(f2, s2); if (!*s3 || !strcmp(*s3, smallest)) readOneStr(f3, s3); } void compareAndOutput(FILE *out, char *s1, char *s2, char *s3) { if (s1 && s2 && !strcmp(s1, s2)) { fprintf(out, "%s\n", s1); return; } if (s1 && s3 && !strcmp(s1, s3)) { fprintf(out, "%s\n", s1); return; } if (s2 && s3 && !strcmp(s2, s3)) { fprintf(out, "%s\n", s2); return; } } int main() { FILE *f1 = fopen("file1.txt", "r"); FILE *f2 = fopen("file2.txt", "r"); FILE *f3 = fopen("file3.txt", "r"); FILE *out = fopen("out.txt", "w"); char *s1 = NULL; char *s2 = NULL; char *s3 = NULL; do { readStrs(f1, f2, f3, &s1, &s2, &s3); compareAndOutput(out, s1, s2, s3); } while(s1 && s2 && s3); return 0; }
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
i am not allowed to use feof
i was told to use flags
flag[0]=scanf...
i cant see how to use this flag array in the while condition
Wait a minute, and I will rewrite mine to not use feof() - it would have helped to have ALL the information before starting, of course.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
No feof().Code:#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> struct FileData { FILE *f; char *s; }; void readOneStr(struct FileData *fd) { char temp[100]; if (fgets(temp, sizeof(temp), fd->f)) { temp[strlen(temp)-1] = 0; fd->s = realloc(fd->s, strlen(temp)+1); strcpy(fd->s, temp); } else { free(fd->s); fd->s = NULL; } } char *findSmallest(char *s1, char *s2, char *s3) { char *smallest = s1; if (!smallest || (s2 && (strcmp(s2, smallest) < 0))) { smallest = s2; } if (!smallest || (s3 && (strcmp(s3, smallest) < 0))) { smallest = s3; } return smallest; } void readStrs(struct FileData *f) { char *smallest = findSmallest(f[0].s, f[1].s, f[2].s); int i; for(i = 0; i < 3; i++) if (!f[i].s || !strcmp(f[i].s, smallest)) readOneStr(&f[i]); } void compareAndOutput(FILE *out, struct FileData *f) { if (f[0].s && f[1].s && !strcmp(f[0].s, f[1].s)) { fprintf(out, "%s\n", f[0].s); return; } if (f[0].s && f[2].s && !strcmp(f[0].s, f[2].s)) { fprintf(out, "%s\n", f[0].s); return; } if (f[1].s && f[2].s && !strcmp(f[1].s, f[2].s)) { fprintf(out, "%s\n", f[1].s); return; } } int main() { struct FileData f[3] = { NULL }; FILE *out = fopen("out.txt", "w"); f[0].f = fopen("file1.txt", "r"); f[1].f = fopen("file1.txt", "r"); f[2].f = fopen("file1.txt", "r"); do { readStrs(f); compareAndOutput(out, f); } while(f[0].s && f[1].s && f[2].s); return 0; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.