我已经用普通的 ifstreams 和我正在使用的当前 boost:iostream 尝试了以下代码,两者都有相同的结果。
它旨在将文件从 physfs 加载到内存中,然后将其传递给处理程序进行处理(例如图像、音频或数据)。目前,当调用 c_str 时,它只返回文件的一小部分。
PhysFS::FileStream file("Resources/test.png" , PhysFS::OM_READ);
if(file.is_open()) {
String* theFile;
theFile = new String((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
String::iterator it;
for ( it=theFile->begin() ; it < theFile->end(); it++ ) {
std::cout << *it;
} // Outputs the entire file
std::cout << theFile->c_str(); // Outputs only the first few lines
}
迭代器循环按预期输出整个 png 文件,但 c_str 调用仅返回前几个字符 (\211PNG)。
很长一段时间以来,我一直在尝试此代码的变体,但没有成功。有任何想法吗?