Function fread not terminating string by \0

前端 未结 3 2090
遥遥无期
遥遥无期 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条回答
  •  借酒劲吻你
    2020-12-17 21:19

    The man page for fread says nothing about adding a terminating zero at the end of the file.

    If you want to be safe, initialize all the bytes in your c array to be zero (via bzero or something like that) and when you read in, you'll then have a terminating null.

    I've linked the two man pages for fread and bzero and I hope that helps you out.

提交回复
热议问题