简体   繁体   中英

C How to read from a binary file to a string?

I have the function bellow that reads a line from a file and saves it into a string regardless BUFF_SIZE length. I would like to expand its usage by making it capable of reading binary files. Is it possible to do this by only using the functions : open/read/malloc/free ?

static int read_to_stock(int const fd, char **stock){
   char   *buff;
   int  ret;

   if (fd < 0 || BUFF_SIZE < 0)
      return (-1);
   if (!(buff = (char *)malloc(sizeof(*buff) * (BUFF_SIZE + 1))))
      return (-1);
   ret = read(fd, buff, BUFF_SIZE);
   if (ret > 0)
   {
      buff[ret] = '\0';
      *stock = ft_strjoin(*stock, buff); //joins two strings
   }
   free(buff);
   return (ret);
}

int get_next_line(int const fd, char ** line){
   static char  *stock = NULL;
   char     *bn;
   int         ret;

   if (!stock && (stock = (char *)malloc(sizeof(*stock))) == NULL)
      return (-1);
   bn = ft_strchr(stock, '\n'); //search in string 'stock' for char '\n' and returns a pointer to it.
   while (bn == NULL)
   {
      ret = read_to_stock(fd, &stock);
      if (ret == 0)
         return (0);
      if (ret < 0)
         return (-1);
      else
         bn = ft_strchr(stock, '\n'); //search in string 'stock' for char '\n' and returns a pointer to it.
   }
   *line = ft_strsub(stock, 0, ft_strlen(stock) - ft_strlen(bn)); //subs-tracts a part of a string(string_from, starting_index, length)
   if (!*line)
      return (-1);
   stock = ft_strdup(bn + 1); //duplicate a string
   return (1);
}

int main(void){
   int  fd;
   char *line;

   fd = open("file1", O_RDONLY);
   while (get_next_line(fd, &line) > 0)
   {
      printf("%s\n", line);
   }
   close(fd);
   return (0);
}

Reading from binary files can definitely be performed via the functions open , read , malloc , free on platforms that support them, but be aware that binary files may contain '\\0' bytes.

Therefore you cannot rely on the string functions that you use such as ft_strchr , ft_join , printf etc. These handle C strings and will stop on embedded '\\0' bytes. You need to handle blocks of bytes directly, not C strings.

Furthermore, handling binary files as sequences of lines is not recommended: line endings may be transcoded by the runtime library: for instance CR LF pairs are converted to single '\\n' bytes when reading files in text mode on Windows and vice versa when writing to stdout where text mode is the default. Other such transcoding may occur on other platforms. Sadly, not every computer is a unix box.

Finally, even if no such transcoding occurs, the file may not end with a '\\n' and adding one in the output may corrupt the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM