File read using POSIX API's

后端 未结 4 1444
死守一世寂寞
死守一世寂寞 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:58

    1, you can get the file size with stat(filename, &stat), but define the buffer to page size is just fine

    2, first, there is no NULL character after "Hello", it must be accident that the stack area you allocated was 0 before your code executed, please refer to APUE chapter 7.6. In fact you must initialize the local variable before using it.

    I tried to generate the text file with vim, emacs and echo -n Hello > file-to-buff.txt, only vim adds a line break automatically

提交回复
热议问题