Pour la premiere a la derniere ligne du premier fichier (ligneCouranteFichier1) faire tableau1 = split(ligneCouranteFichier1); Pour la premiere a la derniere ligne du deuxieme fichier (ligneCouranteFichier2) faire tableau2 = split(ligneCouranteFichier2); Si (diffLine(tableau1, tableau2)) // les lignes sont identiques Sinon // les lignes sont differentes fin si fni pour fin pour
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionfprintf(fp,"%s",s); //le fichier "f.txt" fprintf(fp2,"%s",s); //le fichier "f2.txt"
Effectivement il ne te reste plus qu'à coder "same_word".
c:\tester_\debug> tester_.exe f.txt f1.txt
[...] de plus moi je n'utilise pas l'exécution ar ligne de commande car j'utilise l'environnement visual studio 2008 pour la compilation et l'exécution ?
Pouvez vous posté une version de votre solution sans utiliser argc et argv ?
#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); } printf("nb ret=%d\n", nb); return res; } int main() { char** res = NULL; char** pos = NULL; char input[32]; int i ; for (i 1; i <3; i++) { sprintf(input,"f%d.txt",i); res = compare_file("f.txt",input); pos = res; printf("Comparing f.txt and %s:\n", input); while (pos && *pos) { printf("%s\n", *pos); pos++; } free_tab(res); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *f,*f2 ; char s[1000], s2[1000]; f = fopen("f.txt", "r"); f2 = fopen("f2.txt", "r"); while(fgets(s, 1000, f)!= NULL) { rewind(f2); while(fgets(s2, 1000, f2)!= NULL) if(strcmp(s, s2)) printf("%s\n",s); } fclose(f); fclose(f2); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { 8. FILE *f,*f2 ; char s[1000], s2[1000]; f = fopen("f.txt", "r" ); f2 = fopen("f2.txt", "r" ); while(fgets(s, 1000, f)!= NULL) { rewind(f2); while(fgets(s2, 1000, f2)!= NULL) if(strcmp(s, s2)) printf("%s\n",s); } fclose(f); fclose(f2); return 0; }
Debug assertion Failed!
Prpgram :
File : f\dd\vctools\crt_bld\self_x86\crt\src\fgets.c
Line : 57
Expression : (str != NULL)
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *f, *f2; char s[1000], s2[1000]; char* filename1 = "f.txt"; char* filename2 = "f2.txt"; f = fopen(filename1, "r" ); if (f == NULL) { printf("Unable to open file %s\n", filename1); return 1; } f2 = fopen(filename2, "r" ); if (f2 == NULL) { printf("Unable to open file %s\n", filename2); return 1; } while (fgets(s, 1000, f) != NULL) { rewind(f2); while (fgets(s2, 1000, f2) != NULL) if (strcmp(s, s2)) printf("%s\n",s); } fclose(f); fclose(f2); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *f, *f2; char s[1000], s2[1000]; char* filename1 = "z.txt"; char* filename2 = "z2.txt"; f = fopen(filename1, "r" ); if (f == NULL) { perror(filename1); return 1; } f2 = fopen(filename2, "r" ); if (f2 == NULL) { perror(filename1); g return 1; } while (fgets(s, 1000, f) != NULL) { rewind(f2); while (fgets(s2, 1000, f2) != NULL) if (strcmp(s, s2)) printf("%s\n",s); } fclose(f); fclose(f2); return 0; }