我有一個程序,讀取包含列表的.dat文件:讀取字符顯示所有的字符,隨後一些垃圾字符
removepeer 452
addpeer 6576
removepeer 54245
在某些時候,它讀出垃圾文字:H
這裏是我的代碼的一部分,我發現故障:
getline(abc, info, '\n'); //data here displays pretty fine
int count = info.size();
char text[count];
for(int a=0; a<count; a++){
text[a] = data[a];
}
cout << text << endl; //Some rubbish text found in some printout!
它打印出的最後一行其次是一些垃圾文字
問題幾乎可以肯定是一個沒有終止的字符串,但是什麼是'數據'?數據[a]'保證在該對象的範圍之內嗎?爲什麼不直接打印'info'? –