is.read (buffer,length); if (is) std::cout << 'all characters read successfully.'; else std::cout << 'error: only ' << is.gcount() << ' could be read'; is.close(); // ...buffer contains the entire file... delete[] buffer; } return 0; }123456789101112131415161718192021222324252...
read(buffer, sizeof(buffer)); // 检查是否成功读取了整个缓冲区 if (file.gcount() != sizeof(buffer)) { std::cerr << "Failed to read the entire buffer" << std::endl; return 1; } 通过这种方式,你可以确保在 ifstream::read 调用失败时能够适当地处理错误情况。
std::string filename("filei o"); std::ifstream file(filename.c _str(), std::ios::in|st d::ios::binary std::ios::ate); std::vector<cha r>vbuf(file.tel lg()); file.seekg(0, std::ios::beg); file.read(&vbuf[0], vbuf.size()); print(vbuf, std::cout); return 0; ...