0

このコードを Python で C++ に変換します。

content = file(filename, "rb").read()

これは c++ のコードです。

ifstream file;
file.open(filename, fstream::binary);

file.seekg (0, ios::end);
long fileLength = file.tellg();
file.seekg(0, ios_base::beg);

char *content = new char[fileLength];
file.read(content, fileLength);

Pythonコードを実行すると、コンテンツに長い文字列(500文字〜)が表示されますが、C++コードは4文字しか返しません。

なにか提案を?

ありがとう

4

1 に答える 1

2

ファイル全体を読み取る最も簡単な方法は次のとおりです。

std::string content(
    std::istreambuf_iterator<char>(std::ifstream(filename, std::fstream::binary).rdbuf()),
    std::istreambuf_iterator<char>());
于 2012-09-05T09:14:34.633 に答える