Consider the following piece of code for reading the contents of the file into a buffer
#include
#include
#include
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.