./tester f.txt f1.txt
b d
c d
a b d
b c d
a c d
a b c d
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 1024 char* strndup(const char *str, size_t n) { char *ret = malloc((n+1) * sizeof (char)); if (!ret) exit(0); strncpy(ret, str, n); ret[n] = 0; return ret; } void free_tab(char** t) { char** head = t; while (t && *t) { free(*t); t++; } free(head); } char** get_word(const char* str) { char* pos = NULL; char** res = NULL; int nb = 0; do { int size = 0; pos = strchr(str, ' '); if (pos) size = pos - str; else size = strlen(str) - 1; /* - 1 to get rid of \n */ nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb -1] = strndup(str, size); str = pos + 1; } while (pos); res[nb] = 0; return res; } int is_same(const char* const s1, const char* const s2) { char **t1, **t2; int ret = 0; t1 = get_word(s1); t2 = get_word(s2); if (t1 && t2) { char** tab1 = t1; char** tab2 = t2; int nb = 0; int size = 0; while (tab1 && *tab1) { tab2 = t2; while (tab2 && *tab2) { if (!strcmp(*tab1, *tab2)) nb++; tab2++; } tab1++; size++; } ret = (nb >= size); free_tab(t1); free_tab(t2); } return ret; } char** compare_file(const char* filename1, const char* filename2) { FILE *f, *f1; char s[MAX_SIZE], s1[MAX_SIZE]; int a; char** res = NULL; int nb = 0; f = fopen(filename1, "r"); f1 = fopen(filename2, "r"); if (f && f1) { while (fgets(s, MAX_SIZE, f)) { a = 0; rewind(f1); while (fgets(s1, MAX_SIZE, f1)) { if (is_same(s, s1)) { a = 1; break; } } if (!a) { nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb - 1] = strndup(s, strlen(s) - 1); /* Get rid of the \n */ res[nb] = 0; } } fclose(f); fclose(f1); } return res; } char** copy_tab(char** tab) { char** res = NULL; int nb = 0; while (tab && *tab) { nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb - 1] = strndup(*tab, strlen(*tab)); res[nb] = 0; tab++; } return res; } char** compute_intersect(char** tab1, char** tab2) { if (!tab2) return NULL; if (!tab1) return copy_tab(tab2); /* Ici finir de coder l'intersection */ return NULL; } void display_tab(char** tab) { while (tab && *tab) { printf("%s\n", *tab); tab++; } } int main() { char** res = NULL; char** intersect = NULL; char input[32]; int i; for (i = 1; i <= 3; i++) { sprintf(input,"f%d.txt",i); res = compare_file("f.txt",input); /* temp */ printf("Comparing f.txt and %s:\n", input); display_tab(res); /* ! temp */ intersect = compute_intersect(intersect, res); free_tab(res); } printf("Final result:\n"); display_tab(intersect); free_tab(intersect); return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 1024 char* strndup(const char *str, size_t n) { char *ret = malloc((n+1) * sizeof (char)); if (!ret) exit(0); strncpy(ret, str, n); ret[n] = 0; return ret; } void free_tab(char** t) { char** head = t; while (t && *t) { free(*t); t++; } free(head); } char** get_word(const char* str) { char* pos = NULL; char** res = NULL; int nb = 0; do { int size = 0; pos = strchr(str, ' '); if (pos) size = pos - str; else size = strlen(str) - 1; /* - 1 to get rid of \n */ nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb -1] = strndup(str, size); str = pos + 1; } while (pos); res[nb] = 0; return res; } int is_same(const char* const s1, const char* const s2) { char **t1, **t2; int ret = 0; t1 = get_word(s1); t2 = get_word(s2); if (t1 && t2) { char** tab1 = t1; char** tab2 = t2; int nb = 0; int size = 0; while (tab1 && *tab1) { tab2 = t2; while (tab2 && *tab2) { if (!strcmp(*tab1, *tab2)) nb++; tab2++; } tab1++; size++; } ret = (nb >= size); free_tab(t1); free_tab(t2); } return ret; } char** compare_file(const char* filename1, const char* filename2) { FILE *f, *f1; char s[MAX_SIZE], s1[MAX_SIZE]; int a; char** res = NULL; int nb = 0; f = fopen(filename1, "r"); f1 = fopen(filename2, "r"); if (f && f1) { while (fgets(s, MAX_SIZE, f)) { printf("s=%s\n",s); a = 0; rewind(f1); while (fgets(s1, MAX_SIZE, f1)) { printf("s1=%s\n",s1); if (is_same(s, s1)) { printf("eagle=%s\n",s); a = 1; break; } } if (!a) { printf("non egale=%s\n",s); nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb - 1] = strndup(s, strlen(s) - 1); /* Get rid of the \n */ res[nb] = 0; } } fclose(f); fclose(f1); } return res; } char** copy_tab(char** tab) { char** res = NULL; int nb = 0; while (tab && *tab) { nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb - 1] = strndup(*tab, strlen(*tab)); res[nb] = 0; tab++; } return res; } char** compute_intersect(char** tab1, char** tab2) { if (!tab2) return NULL; if (!tab1) return copy_tab(tab2); /* Ici finir de coder l'intersection */ return NULL; } void display_tab(char** tab) { while (tab && *tab) { printf("%s\n", *tab); tab++; } } int main() { char** res = NULL; char** intersect = NULL; char input[32]; int i; for (i = 1; i <= 3; i++) { printf("%d\n",i); sprintf(input,"f%d.txt",i); printf("%s\n",input); res = compare_file("f.txt",input); /* temp */ printf("Comparing f.txt and %s:\n", input); display_tab(res); /* ! temp */ // intersect = compute_intersect(intersect, res); free_tab(res); } printf("Final result:\n"); display_tab(intersect); free_tab(intersect); return 0; }
1
f1.txt
s=a b
s1=a
s1=d a
s1=c a
s1=b c a
retour=a b
s=a c
s1=a
s1=d a
s1=c a
eagle=a c
s=a d
s1=a
s1=d a
eagle=a d
s=b c
s1=a
s1=d a
s1=c a
s1=b c a
eagle=b c
s=b d
s1=a
s1=d a
s1=c a
s1=b c a
retour=b d
s=c d
s1=a
s1=d a
s1=c a
s1=b c a
retour=c d
s=a b c
s1=a
s1=d a
s1=c a
s1=b c a
retour=a b c
s=a b d
s1=a
s1=d a
s1=c a
s1=b c a
retour=a b d
s=b c d
s1=a
s1=d a
s1=c a
s1=b c a
retour=b c d
s=a c d
s1=a
s1=d a
s1=c a
s1=b c a
retour=a c d
s=a b c d
s1=a
Process returned -1073741819 <0 * C0000005>
Press any key to continue.
Une exception win 32 non gérée s'est produite dans comparaison .exe .
1
f1.txt
s=a b
s1=a
s1=d a
s1=c a
s1=b c a
retour=a b
s=a c
s1=a
s1=d a
s1=c a
eagle=a c
s=a d
s1=a
s1=d a
eagle=a d
s=b c
s1=a
s1=d a
s1=c a
s1=b c a
eagle=b c
s=b d
s1=a
s1=d a
s1=c a
s1=b c a
retour=b d
s=c d
s1=a
s1=d a
s1=c a
s1=b c a
retour=c d
s=a b c
s1=a
s1=d a
s1=c a
s1=b c a
retour=a b c
s=a b d
s1=a
s1=d a
s1=c a
s1=b c a
retour=a b d
s=b c d
s1=a
s1=d a
s1=c a
s1=b c a
retour=b c d
s=a c d
s1=a
s1=d a
s1=c a
s1=b c a
retour=a c d
s=a b c d
s1=a
Process returned -1073741819 <0 * C0000005>
Press any key to continue.
1
f1.txt
s=a b
s1=a
s1=d a
s1=c a
s1=b c a
eagle=a b
s=a c
s1=a
s1=d a
s1=c a
eagle=a c
s=a d
s1=a
s1=d a
eagle=a d
s=b c
s1=a
s1=d a
s1=c a
s1=b c a
eagle=b c
s=b d
s1=a
s1=d a
s1=c a
s1=b c a
non egale=b d
s=c d
s1=a
s1=d a
s1=c a
s1=b c a
non egale=c d
s=a b c
s1=a
s1=d a
s1=c a
s1=b c a
eagle=a b c
s=a b d
s1=a
s1=d a
s1=c a
s1=b c a
non egale=a b d
s=b c d
s1=a
s1=d a
s1=c a
s1=b c a
non egale=b c d
s=a c d
s1=a
s1=d a
s1=c a
s1=b c a
non egale=a c d
s=a b c d
s1=a
s1=d a
s1=c a
s1=b c a
non egale=a b c d
Comparing f.txt and f1.txt:
b d
c d
a b d
b c d
a c d
a b c d
2
f2.txt
s=a b
s1=a
s1=c
s1=b
s1=d
s1=b a
eagle=a b
s=a c
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
s1=b c a
eagle=a c
s=a d
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
eagle=a d
s=b c
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
eagle=b c
s=b d
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
s1=b c a
non egale=b d
s=c d
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
eagle=c d
s=a b c
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
s1=b c a
eagle=a b c
s=a b d
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
s1=b c a
non egale=a b d
s=b c d
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
s1=b c a
non egale=b c d
s=a c d
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
s1=b c a
non egale=a c d
s=a b c d
s1=a
s1=c
s1=b
s1=d
s1=b a
s1=d a
s1=b c
s1=d c
s1=b c a
non egale=a b c d
Comparing f.txt and f2.txt:
b d
a b d
b c d
a c d
a b c d
3
f3.txt
s=a b
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
eagle=a b
s=a c
s1=a
s1=b
s1=c
s1=c a
eagle=a c
s=a d
s1=a
s1=b
s1=c
s1=c a
s1=d a
eagle=a d
s=b c
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
eagle=b c
s=b d
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
s1=b c a
non egale=b d
s=c d
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
s1=b c a
non egale=c d
s=a b c
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
s1=b c a
eagle=a b c
s=a b d
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
s1=b c a
non egale=a b d
s=b c d
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
s1=b c a
non egale=b c d
s=a c d
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
s1=b c a
non egale=a c d
s=a b c d
s1=a
s1=b
s1=c
s1=c a
s1=d a
s1=b a
s1=b c
s1=b c a
non egale=a b c d
Comparing f.txt and f3.txt:
b d
c d
a b d
b c d
a c d
a b c d
Final result:
char* strndup(const char *str, size_t n) { char *ret = malloc((n+1) * sizeof (char)); if (!ret) exit(0); ret = strncpy(ret, str, n); ret[n] = 0; return ret; }
char* strndup(const char *str, size_t n) { char *ret = malloc((n+1) * sizeof (char)); if (!ret) exit(0); ret = strncpy(ret, str, n); ret[n] = 0; return ret; }
char* my_strncpy(char* to, const char* from, size_t size) { char* head = to; while (from && *from && size > 0) { *to = * from; to++; from++; --size; } *to = 0; return head; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 1024 char* strndup(const char *str, size_t n) { char *ret = malloc((n+1) * sizeof (char)); if (!ret) exit(0); strncpy(ret, str, n); ret[n] = 0; return ret; } void free_tab(char** t) { char** head = t; while (t && *t) { free(*t); t++; } free(head); } char** get_word(const char* str) { char* pos = NULL; char** res = NULL; int nb = 0; do { int size = 0; pos = strchr(str, ' '); if (pos) size = pos - str; else size = strlen(str) - 1; /* - 1 to get rid of \n */ nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb -1] = strndup(str, size); str = pos + 1; } while (pos); res[nb] = 0; return res; } int is_same(const char* const s1, const char* const s2) { char **t1, **t2; int ret = 0; t1 = get_word(s1); t2 = get_word(s2); if (t1 && t2) { char** tab1 = t1; char** tab2 = t2; int nb = 0; int size = 0; while (tab1 && *tab1) { tab2 = t2; while (tab2 && *tab2) { if (!strcmp(*tab1, *tab2)) nb++; tab2++; } tab1++; size++; } ret = (nb >= size); free_tab(t1); free_tab(t2); } return ret; } char** compare_file(const char* filename1, const char* filename2) { FILE *f, *f1; char s[MAX_SIZE], s1[MAX_SIZE]; int a; char** res = NULL; int nb = 0; f = fopen(filename1, "r"); f1 = fopen(filename2, "r"); if (f && f1) { while (fgets(s, MAX_SIZE, f)) { a = 0; rewind(f1); while (fgets(s1, MAX_SIZE, f1)) { if (is_same(s, s1)) { a = 1; break; } } if (!a) { nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb - 1] = strndup(s, strlen(s) - 1); /* Get rid of the \n */ res[nb] = 0; } } fclose(f); fclose(f1); } return res; } char** copy_tab(char** tab) { char** res = NULL; int nb = 0; while (tab && *tab) { nb++; res = realloc(res, (nb + 1) * sizeof (char*)); res[nb - 1] = strndup(*tab, strlen(*tab)); res[nb] = 0; tab++; } return res; } char** compute_intersect(char** tab1, char** tab2) { if (!tab2) return NULL; if (!tab1) return copy_tab(tab2); /* Ici finir de coder l'intersection */ return NULL; } void display_tab(char** tab) { while (tab && *tab) { printf("%s\n", *tab); tab++; } } int main() { char** res = NULL; char** intersect = NULL; char input[32]; int i; for (i = 1; i <= 3; i++) { sprintf(input,"f%d.txt",i); res = compare_file("f.txt",input); /* temp */ printf("Comparing f.txt and %s:\n", input); display_tab(res); /* ! temp */ intersect = compute_intersect(intersect, res); free_tab(res); } printf("Final result:\n"); display_tab(intersect); free_tab(intersect); return 0; }
Comparing f.txt and f1.txt:
a b
b d
c d
a b c
a b d
b c d
a c d
a b c d
Comparing f.txt and f2.txt:
a c
b d
a b c
a b d
b c d
a c d
a b c d
Comparing f.txt and f3.txt:
b d
c d
a b c
a b d
b c d
a c d
a b c d
Final result:
Appuyez sur une touche pour continuer...
Comparing f.txt and f1.txt:
b d
c d
a b d
b c d
a c d
a b c d
Comparing f.txt and f2.txt:
b d
a b d
b c d
a c d
a b c d
Comparing f.txt and f3.txt:
b d
c d
a b d
b c d
a c d
a b c d
Final result:
Appuyez sur une touche pour continuer...
Comparing f.txt and f1.txt:
b d
c d
a b d
b c d
a c d
a b c d
Comparing f.txt and f2.txt:
b d
a b d
b c d
a c d
a b c d
Comparing f.txt and f3.txt:
b d
c d
a b d
b c d
a c d
a b c d
Final result:
b d
c d
a b d
b c d
a c d
a b c d
Comparing f.txt and f1.txt:
a b
b d
c d
a b c
a b d
b c d
a c d
Comparing f.txt and f2.txt:
a c
b d
a b c
a b d
b c d
a c d
Comparing f.txt and f3.txt:
b d
c d
a b c
a b d
b c d
a c d
Final result:
Le code que tu viens de copier coller me donne le bon résultat:
Comparing f.txt and f1.txt:
b d
c d
a b d
b c d
a c d
a b c d
Comparing f.txt and f2.txt:
b d
a b d
b c d
a c d
a b c d
Comparing f.txt and f3.txt:
b d
c d
a b d
b c d
a c d
a b c d
Final result:
b d
c d
a b d
b c d
a c d
a b c d
- Pouvez vous poster le code qui vous permet d'obtenir ce résultat ?
- Pouvez vous aussi poster le contenu des fichiers qui vous êtes entrain de les tester ?
- Si vous avez testé ceci sur windows alors vous utilisez quel compilateur ?
Le code que tu viens de copier coller me donne le bon résultat:
Comparing f.txt and f1.txt:
b d
c d
a b d
b c d
a c d
a b c d
Comparing f.txt and f2.txt:
b d
a b d
b c d
a c d
a b c d
Comparing f.txt and f3.txt:
b d
c d
a b d
b c d
a c d
a b c d
Final result:
b d
c d
a b d
b c d
a c d
a b c d
Final result:
b d
c d
a b d
b c d
a c d
a b c d
b d
a b d
b c d
a c d
a b c d
b d
a c d