file - C - Get next line -


( i'm authorized use: malloc, free, read)
i'm trying next line in file buf_size it's returning me wrong value.
so, returned (wrong value, missing firsts chars) value:

3 1 - #include <stdio.h> 1 - dlib.h> 1 - clude "libft/libft.h" 1 - buff_size  32 

my source code:

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include "libft/libft.h" # define buff_size  32  int     get_next_line(int const fd, char **line) {     char buffer[buff_size + 1];     size_t i;     size_t size;      if (!(*line = malloc(buff_size)))         return (-1);     *line[0] = '\0';     while (read(fd, buffer, buff_size) > 0)     {         buffer[buff_size + 1] = '\0';         if (buffer[0] == '\n')             return (1);         if (ft_strchr(buffer, '\n') != null)         {             = 0;             size = ft_strlen(*line);             buffer[buff_size + 1] = '\0';             while (buffer[i] != '\0')             {                 if (buffer[i] == '\n')                 {                     if (!(*line = realloc(*line, + size)))                         return (-1);                     buffer[i] = '\0';                     ft_strncat(*line, buffer, i);                     return (1);                 }                 i++;             }         }         if (!(*line = realloc(*line, buff_size)))             return (-1);         ft_strncat(*line, buffer, buff_size);     }     return (0); }  int     main(void) {     int     fd;     int     ret;     char    *line;      if ((fd = open("main.c", o_rdonly)) < 3 && fd != 0)         return (-1);     printf("%d\n", fd);     ret = get_next_line(fd, &line);     printf("%d - %s\n", ret, line);     ret = get_next_line(fd, &line);     printf("%d - %s\n", ret, line);     ret = get_next_line(fd, &line);     printf("%d - %s\n", ret, line);     ret = get_next_line(fd, &line);     printf("%d - %s\n", ret, line);     return (0); } 

i got compile replacing ft_blah functions string.h equivalents. there many problems.

strings in c null terminated , need byte allocated them null. if want allocate string of single character, have allocate 2 bytes.

next big problem keep trying append tmp has single byte allocated. tmp needs have more memory allocated go.

on theme of memory allocation, buffer never returned there's no need allocate memory on heap. can on stack instead , automatically free'd when function returns. char buffer[2] = "".

you should using strncat, not strcpy, concatenate strings.

it seems ft_strverif copies string? don't know why call before calling strcpy copy string. strcpy(tmp, ft_strverif(buffer)); means buffer needlessly copied twice. worse, copy never free'd leak memory.

i'd suggest eliminating tmp entirely , instead append directly line. use realloc add more memory line, best add memory in chunks or double every time efficiency.

if you're emulating getline note getline takes length of allocated line buffer reason. lets line reused in while(getline...) loop without having free'd every loop iteration. in effect, line becomes reusable buffer.

finally, run -wall or -weverything if compiler supports it. fix all warnings, if seem silly. then, run code using valgrind, tell when you're using uninitialized memory or you're leaking memory. very, helpful.

once that's done, get_next_line becomes simpler.

int get_next_line(int const fd, char **line) {     char buffer[2] = "";      /* allocate line memory if needed */     if( !*line )         *line = malloc(100 * sizeof(char));      /* blank out line */     *line[0] = '\0';      while( read(fd, buffer, 1) > 0 ) {         strncat(*line, buffer, 1);         if( buffer[0] == '\n' )             break;     }      return 0; } 

the exercises left are...

  • protect against overflowing line.
  • don't use static size line.
  • make get_next_line grow line needed.
  • let caller know how large line (like getline does).

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -