File read using POSIX API's

后端 未结 4 1457
死守一世寂寞
死守一世寂寞 2021-02-03 15:03

Consider the following piece of code for reading the contents of the file into a buffer

#include 
#include 
#include 

        
4条回答
  •  无人共我
    2021-02-03 15:51

    Since you want to read the whole file, the best way is to make the buffer as big as the file size. There's no point in resizing the buffer as you go. That just hurts performance without good reason.

    You can get the file size in several ways. The quick-and-dirty way is to lseek() to the end of the file:

    // Get size.
    off_t size = lseek(fd, 0, SEEK_END); // You should check for an error return in real code
    // Seek back to the beginning.
    lseek(fd, 0, SEEK_SET);
    // Allocate enough to hold the whole contents plus a '\0' char.
    char *buff = malloc(size + 1);
    

    The other way is to get the information using fstat():

    struct stat fileStat;
    fstat(fd, &fileStat); // Don't forget to check for an error return in real code
    // Allocate enough to hold the whole contents plus a '\0' char.
    char *buff = malloc(fileStat.st_size + 1);
    

    To get all the needed types and function prototypes, make sure you include the needed header:

    #include  // For fstat()
    #include    // For lseek()
    

    Note that read() does not automatically terminate the data with \0. You need to do that manually, which is why we allocate an extra character (size+1) for the buffer. The reason why there's already a \0 character there in your case is pure random chance.

    Of course, since buf is now a dynamically allocated array, don't forget to free it again when you don't need it anymore:

    free(buff);
    

    Be aware though, that allocating a buffer that's as large as the file you want to read into it can be dangerous. Imagine if (by mistake or on purpose, doesn't matter) the file is several GB big. For cases like this, it's good to have a maximum allowable size in place. If you don't want any such limitations, however, then you should switch to another method of reading from files: mmap(). With mmap(), you can map parts of a file to memory. That way, it doesn't matter how big the file is, since you can work only on parts of it at a time, keeping memory usage under control.

提交回复
热议问题