File read using POSIX API's

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

    For your second question, read don't add automatically a character '\0'. If you consider that your file is a textual file, your must add a '\0' after calling read, for indicate the end of string.

    In C, the end of string is represented by this caracter. If read set 4 characters, printf will read these 4 characters, and will test the 5th: if it's not '\0', it will continue to print until next '\0'. It's also a source of buffer overflow

    For the '\n', it is probably in the input file.

提交回复
热议问题