我想打开一个文件并将其放入一个字符串中。我的代码是:使用ifstream加载文件时出错
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
streampos size;
char * memblock;
ifstream file ("C:\\a\\test.snt", ios::in | ios::binary | ios::ate);
if (file.is_open()){
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
std::string someString(memblock);
if(file.bad()){cout << "Bad\n";}
if(file.fail()){cout << "Fail\n";}
delete[] memblock;
}else{ cout << "Unable to open file";}
return 0;
}
问题是,它不会加载大部分只有一小部分作为这样的“YYYY««««««««îþîþ”。 我想,当用记事本打开++文件看起来像这样Notepad++ http://gyazo.com/9d8dc182d5dfa06610372462831cca0e.png