Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define F_NAME "f.txt" #define F1_NAME "f1.txt" #define F2_NAME "f2.txt" #define F3_NAME "f3.txt" #define MAX_SIZE 1024 int main(void) { FILE *f, *f1,*f2,*f3; char s[MAX_SIZE], s1[MAX_SIZE]; int a; f = fopen(F_NAME, "r"); f1 = fopen(F1_NAME, "r"); f2 = fopen(F2_NAME, "r"); f3 = fopen(F3_NAME, "r"); if(f && f1 && f2 && f3) { for (i=1;i<=3;i++) { 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) printf(s); } fclose(f); fclose(f1); fclose(f2); fclose(f3); } return 0; }
int is_same(const char *s1, const char *s2) { char **t1, **t2; size_t size1 count_word(s1), size2 count_word(s2); int ret = 0; t1 = malloc(size1 * sizeof(char*)); t2 = malloc(size2 * sizeof(char*)); if(t1 && t2) { size_t i, j; int a; get_word(t1, s1); get_word(t2, s2); ret = 1; for(i = 0; i < size1; i++) { a = 0; for(j = 0; j < size2; j++) if(!strcmp(t1[i], t2[j])) a = 1; if(!a) ret = 0; } free_tab(t1, size1), free_tab(t2, size2); } else exit(0); return ret; }
/* Nombre d'élément ayant été trouvé */ nb = 0; for (i = 0; i < size1; i++) for (j = 0; j < size2; j++) if(!strcmp(t1[i], t2[j])) nb++; ret (nb size1);
Est ce que "a c" est contenu dans "a d" ?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define F_NAME "f.txt" #define F1_NAME "f1.txt" #define MAX_SIZE 1024 char * mstrndup(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, size_t len) { if(t) { size_t i; for(i = 0; i < len; i++) free(t[i]); free(t); } } size_t count_word(const char *str) { size_t n = 0; int a; while(*str) { a = 0; while(isalpha((unsigned char) *str) && *str) str++, a = 1; if(a) n++; while(!isalpha((unsigned char) *str) && *str) str++; } return n; } void get_word(char **tab, const char *str) { const char* p = str; int a, i = 0; while(*str) { a = 0; while(isalpha((unsigned char) *p) && *p) p++, a = 1; if(a) tab[i++] = mstrndup(str, p-str); while(!isalpha((unsigned char) *p) && *p) p++; str = p; } } int is_same(const char *s1, const char *s2) { char **t1, **t2; size_t size1 count_word(s1), size2 count_word(s2); int ret = 0; t1 = malloc(size1 * sizeof(char*)); t2 = malloc(size2 * sizeof(char*)); if(t1 && t2) { size_t i, j; int a; get_word(t1, s1); get_word(t2, s2); ret = 1; for(i = 0; i < size1; i++) { a = 0; for(j = 0; j < size2; j++) if(!strcmp(t1[i], t2[j])) a = 1; if(!a) ret = 0; } free_tab(t1, size1), free_tab(t2, size2); } else exit(0); return ret; } int main(void) { FILE *f,*f1 ; char s[MAX_SIZE], s1[MAX_SIZE]; int a; f = fopen(F_NAME, "r"); f1 = fopen(F1_NAME, "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) printf(s); } fclose(f); fclose(f1); } return 0; }
#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; } int main(int argc, char** argv) { char** res = NULL; char** pos = NULL; int i = 0; if (argc <= 2) { printf("Usage : %s file [files]\n", argv[0]); return 1; } for (i = 2; i < argc; ++i) { res = compare_file(argv[1], argv[i]); pos = res; printf("Comparing %s and %s:\n", argv[1], argv[i]); while (pos && *pos) { printf("%s\n", *pos); pos++; } free_tab(res); } return 0; }
- Comment vous exécutez ce programme par ligne des commandes ?
./a.out f.txt f1.txt
- Quel est le résultat de ce programme ?
- Qu'est ce que vous changez dans ce programme si nous n'utilisons pas argc et argv c'est à dire on utilise les noms de fichiers ?
- Est ce que ce programme permet de faire l'intersection entre chaque résultat de chaque comparaison entre deux fichiers et permet t-il de trouver le résultat final ?
En gros : programme liste des fichiers en commençant par f.txt
On laisse à l'utilisateur le choix du nom des fichiers, plutôt que de lui imposer des noms. De plus, il peut mettre autant de fichiers qu'ils le souhaite et pas seulement 2 ou 3.
pos = res
while (pos && *pos)
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; }
printf("Comparing f.txt and %s:\n", input);
lorsque je tape la commande suivante :
./tester_ f.txt f1.txt
avec "tester_" est le nom de programme
alors ceci ne passe pas bien alors j'ai le message suivant:
'.' n'est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier des commandes
- Comment se fait sur windows ?
- quel est le racine à mettre pour lancer un programme par lignes des commandes ?
En réalité dans mon travail ces fichiers sont générés automatiquement et de plus je connais le nombre N qui présentent l'objet de comparaison.
Ici, j'ai mis N 3 pour voir comment on résoudre le problème puisque si la solution marche pour N 3 alors elle marche pour N = 20.
pos = res
- ceci c'est une simple copie d'un tableau dans un autre ?
- pourquoi pas n'utiliser directement "res" sans passer par "pos"
while (pos && *pos)
- Quelle est la différence entre pos et *pos ?
- Quel est le rôle de cette boucle ?
Supposons comme décrit avant on a un fichier de base "f.txt" et N=3 autres fichiers qui sont objet de comparaison alors d'après le dernier code à chaque fois le contenu "res" va être perdu
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; }
printf("Comparing f.txt and %s:\n", input);
printf("Comparing f.txt and %s:\n", input);
c:\tester\debug> tester f.txt f1.txt
#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(int argc, char** argv) { char** res = NULL; char** pos = NULL; int i = 0; if (argc <= 2) { printf("Usage : %s file [files]\n", argv[0]); return 1; } for (i = 2; i < argc; ++i) { res = compare_file(argv[1], argv[i]); pos = res; printf("Comparing %s and %s:\n", argv[1], argv[i]); while (pos && *pos) { printf("%s\n", *pos); pos++; } free_tab(res); } return 0; }