ファイル全体をメモリに読み込み、 C++ に配置する必要がありますstd::string
。
それを a に読み込むとしたらchar[]
、答えは非常に簡単です。
std::ifstream t;
int length;
t.open("file.txt"); // open input file
t.seekg(0, std::ios::end); // go to the end
length = t.tellg(); // report location (this is the length)
t.seekg(0, std::ios::beg); // go back to the beginning
buffer = new char[length]; // allocate memory for a buffer of appropriate dimension
t.read(buffer, length); // read the whole file into the buffer
t.close(); // close file handle
// ... Do stuff with buffer here ...
今、まったく同じことをしたいのですが、 のstd::string
代わりに を使用しchar[]
ます。ループを避けたい、つまりしたくない:
std::ifstream t;
t.open("file.txt");
std::string buffer;
std::string line;
while(t){
std::getline(t, line);
// ... Append line to buffer and go on
}
t.close()
何か案は?