Bonjour, j'essaye de re-coder la fonction getline et j'aimerais une petite aide à propos d'une partie... Quand j'appelle plusieurs fois ma fonction, mon fonction est lu sauf que si le read = 0 et que je continue à appeler ma fonction, mon programme segfault. Je ne vois pas comment faire ma condition me permettant de sortir de mon programme une fois que le programme a fini de lire toutes les lignes... Si quelqu'un pouvait me donner une idée, ça m'aiderait beaucoup, merci !

Le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
char    *get_next_line(const int fd)
{
  static char   buffer[READ_SIZE];
  static int    i = 0;
  char          *str;
  int           j;
 
  j = 0;
  if ((str = malloc(sizeof(char) * READ_SIZE)) == NULL)
    return (NULL);
  if (read(fd, buffer, READ_SIZE) > 0)
    {
      while (1)
        {
          if (buffer[i] == '\n')
            {
              i++;
              return (str);
            }
          if (buffer[i] == '\0')
            {
              i = 0;
              str = my_realloc(str, READ_SIZE);
              if (read(fd, buffer, READ_SIZE) ==  0)
		{
                  if (str[0] != '\0')
                    return (str);
                  return (NULL);
		}
            }
          str[j] = buffer[i];
          j = j + 1;
          i = i + 1;
	}
      free(str);
    }
}
 
int     main(int argc, char **argv)
{
  int   fd;
  if (READ_SIZE == 0)
    return (-1);
  fd = open(argv[1], O_RDONLY);
  printf("%d\n", fd);
  printf("%s\n", get_next_line(fd));
  printf("%s\n", get_next_line(fd));
  printf("%s\n", get_next_line(fd));
}