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
| #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int check(int i, char buffer[2000000])// détermine si le bufferde i est égal à o, . ou \n et assigne une valeur
{
if (buffer[i] == 'o')
return (2);
else if (buffer[i] == '.')
return(1);
else if (buffer[i] == '\n')
return (0);
}
int print(int stock, char **cpy, int x, int y)// affiche et stocke le caractère dans l'emplacement de cpy
{
if (stock == 1)
{
cpy[x][y] = '.';
printf("%c", cpy[x][y]);
}
if (stock == 2)
{
cpy[x][y] = 'o';
printf("%c", cpy[x][y]);
}
if (stock == 0)
{
cpy[x][y] = '\n';
printf("%c", cpy[x][y]);
}
}
char **bsq(int col, int i, char buffer[2000000], int x)//parcours le buffer[i], malloc et appel les fonction précédentes
{
char **cpy;
int y;
int stock;
y = 0;
cpy = malloc(sizeof(char*) * 100);
while(buffer[i])
{
cpy[x] = malloc(sizeof(char) * col);
stock = check(i, buffer);
print(stock, cpy, x, y);
//printf("%c", cpy[0][1]) fonctionne
//printf("%c", cpy[2][7]) segfault
(stock == 1 || stock == 2)?y++:0;
printf("%c", cpy[x][y - 1]);
if (stock == 0)
{
x++;
y = 0;
}
i++;
}
//printf("%c", cpy[0][1])n'affiche rien
//printf("%c", cpy[47][38]);n'affiche rien
free(cpy);
}
int read_that(char *filepath, int i)// parcours le fichier pour trouver les nombre de colonnes et de lignes
{
int fd;
char buffer[2000000];
int lign;
int col;
fd = open(filepath, O_RDONLY);
i = read(fd, buffer, 2000000);
i = 0;
lign = 1;
col = 1;
while (buffer[i++] != '\n')
col++;
i = 0;
while(buffer[i] != '\0')
{
if (buffer[i] == '\n')
lign++;
i++;
}
bsq(col, 0, buffer, 0);
}
int main(int ac, char **av)
{
int i;
read_that(av[1], i);
} |
Partager