New to files in C, trying to read a file via fread
Here's the content of the file:
line1 how
Code used:
char c[6];
fread(c,1,5,f1)
When outputting var 'c', the contents appear with a random character at the end (eg: line1*)
Does fread not terminate the string or am I missing something?
No. The fread
function simply reads a number of elements, it has no notion of "strings".
fgets
/ fscanf
insteadPersonally I would go with fgets
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With