Function fread not terminating string by \0

前端 未结 3 2097
遥遥无期
遥遥无期 2020-12-17 20:55

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];         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 21:16

    No. The fread function simply reads a number of elements, it has no notion of "strings".

    • You can add the NUL terminator yourself
    • You can use fgets / fscanf instead

    Personally I would go with fgets.

提交回复
热议问题