일부 파일 io를 작성하고 아래 테스트를 만들었지 만 testoutput2.txt가 실행 된 후 testinputdata.txt와 같을 것이라고 생각 했습니까?출력 데이터가 입력 데이터와 같지 않음
testinputdata.txt :
some plain
text
data with
a number
42.0
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