因此,我试图编写一个C++程序,将整个文件读入内存(二进制)。内存块显然是个char数组。文件读取成功,但是当我遍历值,其中一些是不是字节,像4294967295按字节读取文件字节,给出了损坏的数据
我真的搜查,尝试了一切,但没有任何工程。 值得一提的是,在阅读文件(这是文本)时,我没有这个问题。
这里是代码:
char* XFile;
ifstream::pos_type Size;
bool LoadFile(string FileName)
{
cout << "Opening File: " << FileName << endl;
ifstream FS(FileName.c_str(),ios::in | ios::binary | ios::ate);
if (!FS.is_open())
return false;
Size = FS.tellg();
cout << "File Size: " << Size << " bytes" << endl;
XFile = new char[Size];
FS.seekg(0, ios::beg);
FS.read(XFile,Size);
for (int i = 0; i < 100; i++) // This loop is to test the read bytes (for example purposes)
cout << static_cast<unsigned>(XFile[i]) << "\n";
FS.close();
return true;
}
有一件事要提,我使用Ubuntu 13.04和g ++可能是重要的4.7 – 2013-05-10 16:11:35
是什么'的sizeof(无符号)'我不认为你得到一个char那里。 – lcs 2013-05-10 16:14:00