char* readFromFile(char* location)
{
int total = 0;
ifstream ifile = ifstream(location);
ifile.seekg(0, ifile.end);
total = ifile.tellg();
cout << "Total count" << total << endl;
char* file = new char[total+1];
ifile.seekg(0, ifile.beg);
ifile.read(file, total+1);
cout <<"File output" << endl<< file << "Output end"<<endl;
return file;
}
這裏是打印文件數據,但它也附加了一些垃圾數值。我應該如何解決它?C++ ifstream在從文本文件中讀取數據時追加垃圾數據
Null終止你的字符串? –