我做了一個示例項目,將文件讀入緩沖區(qū)。當我使用tellg()函數(shù)時,它給我的值大于實際從文件中讀取的read函數(shù)的值。我認為有一個錯誤。這是我的代碼:編輯:void read_file (const char* name, int *size , char*& buffer){ ifstream file; file.open(name,ios::in|ios::binary); *size = 0; if (file.is_open()) { // get length of file file.seekg(0,std::ios_base::end); int length = *size = file.tellg(); file.seekg(0,std::ios_base::beg); // allocate buffer in size of file buffer = new char[length]; // read file.read(buffer,length); cout << file.gcount() << endl; } file.close();}主要:void main(){ int size = 0; char* buffer = NULL; read_file("File.txt",&size,buffer); for (int i = 0; i < size; i++) cout << buffer[i]; cout << endl; }
tellg()函數(shù)給出錯誤的文件大???
慕的地6264312
2019-10-11 14:43:35