我已经尝试了以下代码与正常的ifstreams和当前的boost:我使用的iostream,都有相同的结果。C++ c_str不返回整个字符串
它旨在从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)。
我一直在尝试此代码的变体很长时间没有成功。有任何想法吗?
我该如何解决这个问题?我对C风格字符串没有太多经验 – 2009-10-12 16:59:58
不要使用c_str() - 使用std :: string操作 – Mark 2009-10-12 17:07:04
我正在使用需要c字符串输入的库。 – 2009-10-12 17:09:37