Printf printing garbage after read() call. The offset is always printed as 0

后端 未结 3 1480
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 07:34
#include 
#include 
#include 
#include 
#include 

int main()
{
    int file;
    of         


        
3条回答
  •  甜味超标
    2020-12-12 08:11

    You need to leave room for an end of line character, '\0';

    So make char buffer[19]; char buffer[20]; then also add buffer[19] = '\0'; - remember it's zero based counting. Then it shouldn't have the garbage data.

    The reason is because printf doesn't know where the end is of the character array. So it keeps printing until it finds a '\0' in garbage memory.

提交回复
热议问题