私はいくつかのファイルioを作成していますが、以下のテストを作成しましたが、testoutput2.txtはtestinputdata.txtと同じであると思いましたか?出力データが入力データと同じでない
testinputdata.txt:
some plain
text
data with
a number
42.0
(その別々のライン上のいくつかのエディタではなく、1行に他の人でそのすべて)testoutput2.txt
some plain
ऀ琀攀砀琀ഀഀ
data with
愀 渀甀洀戀攀爀ഀഀ
42.0
int main()
{
//Read plain text data
std::ifstream filein("testinputdata.txt");
filein.seekg(0,std::ios::end);
std::streampos length = filein.tellg();
filein.seekg(0,std::ios::beg);
std::vector<char> datain(length);
filein.read(&datain[0], length);
filein.close();
//Write data
std::ofstream fileoutBinary("testoutput.dat");
fileoutBinary.write(&datain[0], datain.size());
fileoutBinary.close();
//Read file
std::ifstream filein2("testoutput.dat");
std::vector<char> datain2;
filein2.seekg(0,std::ios::end);
length = filein2.tellg();
filein2.seekg(0,std::ios::beg);
datain2.resize(length);
filein2.read(&datain2[0], datain2.size());
filein2.close();
//Write data
std::ofstream fileout("testoutput2.txt");
fileout.write(&datain2[0], datain2.size());
fileout.close();
}
'std :: ios_base :: binary'で開かれたバイナリファイルでのみ' tellg'があなたに実際の文字数を与えることが期待できます。 (実際には、標準でも* *が保証されているかどうかはわかりませんが、テキストファイルの場合、実際には動作しない実装が存在します)。 – celtschk
これはstd :: ios :: binaryで開いたときに動作します。ありがとう – bitgregor