1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 200
int count(FILE *f); /* compte le nombre de lignes */
char **create(int n); /* Crée le tableau à deux dimensions */
FILE *open(char *name, char *mode); /* Ouverture sécuritaire du fichier */
int read(FILE *f, char **tab, int lines); /* Lis et ajoute les lignes dans le tableau */
void display(char **tab, int taille); /* Affiche le tableau */
void freec(char **tab, int taille); /* libération mémoire de chaque chaine */
int is_present(char **tab, char *chaine, int taille); /* vérifier la présence de chaine dans tab */
void write(FILE *f, char **tab, int taille);
int main(void)
{
FILE *fic = open("test.txt", "r");
int nb_line = count(fic);
int t;
char **res = create(nb_line);
t = read(fic, res, nb_line);
display(res, t);
fic = open("test_cp.txt", "w");
write(fic, res, t);
freec(res, nb_line);
free(res);
return 0;
}
char **create(int n)
{
char **tab = NULL;
int i;
tab = malloc(sizeof(char *) * n);
if (tab == NULL)
exit(EXIT_FAILURE);
for (i=0; i<n; i++)
{
tab[i] = malloc(MAX);
if (tab[i] == NULL)
exit(EXIT_FAILURE);
}
return tab;
}
FILE *open(char *name, char *mode)
{
FILE *file = fopen(name, mode);
if (file == NULL)
{
perror("");
exit(EXIT_FAILURE);
}
return file;
}
int count(FILE *f)
{
char *p = NULL;
char line[MAX] = {0};
int counter = 0;
while ((p=fgets(line, MAX, f)) != NULL)
counter += 1;
fseek(f, 0, 0);
return counter;
}
int read(FILE *f, char **tab, int lines)
{
char *p = NULL;
char line[MAX];
size_t n;
int i, j=0;
for (i=0; i<lines; i++)
{
p = fgets(line, MAX, f);
if (p != NULL)
{
p = strchr(line, '\n');
if (p != NULL)
*p = '\0';
if (!is_present(tab, line, j))
{
n = strlen(line) + 1;
p = memcpy(tab[j], line, n);
if (p == NULL)
{
fprintf(stderr, "Error allocation");
exit(EXIT_FAILURE);
}
j += 1;
}
}
}
fclose(f);
return j;
}
void display(char **tab, int taille)
{
int i;
for (i=0; i<taille; i++)
printf("%s\n", tab[i]);
}
void freec(char **tab, int taille)
{
int i;
for (i=0; i<taille; i++)
if(tab[i] != NULL)
free(tab[i]);
}
int is_present(char **tab, char *chaine, int taille)
{
int i, n;
if (taille == 0)
return 0;
for (i=0; i<taille; i++)
{
n = strcmp(tab[i], chaine);
if (n == 0)
return 1;
}
return 0;
}
void write(FILE *f, char **tab, int taille)
{
char *p = NULL;
int i;
size_t n;
for (i=0; i<taille; i++)
{
p = strchr(tab[i], '\0');
if (p != NULL)
*p = '\n';
n = strlen(tab[i]);
fwrite(tab[i], 1, n, f);
}
fclose(f);
} |
Partager