#include <fstream>
int main()
{
// compress
std::ifstream inFile("test.input");
std::ofstream outFile("test.compressed");
char c;
while(inFile >> c)
outFile << c + 1;
// decompress
std::ifstream inFile2("test.compressed");
std::ofstream outFile2("test.output");
while(inFile2 >> c)
outFile2 << c - 1;
// close
inFile.close();
outFile.close();
inFile2.close();
outFile2.close();
return 0;
}
이것이 내 코드입니다. 아마도 내가 이해하지 못했던 무언가가있을 것입니다. 왜냐하면 저에게 test.input
은 test.output
과 동일해야하지만 그렇지 않습니다.파일을 열고 모든 문자를 수정 한 다음 역순으로 원래 파일을 출력하지 않습니다.
열고하려고하는 경우 '파일에 당신은 이미'먼저 플러시로 처리 ofstream'이 ifstream'. 압축 해제와 관련된 것을 시작하기 전에'outFile.flush()'를 호출하십시오. – StoryTeller
기본 운영체제와 파일 시스템이 모든 데이터를 사용자에게 출력할지 여부를 결정할 수 있습니다. 읽기를 위해 열 때까지 "test.compressed"첫 번째 임시 출력 파일. –
더 나아가, 필요한 것보다 오랫동안 물건을 보관하지 마십시오. 압축 코드를 중괄호로 묶거나 (또는 다른 함수로 이동) 압축 해제 코드에 대해 동일하게 수행하십시오. 또한 스트림 소멸자를 사용하여 파일을 닫을 수 있으므로 명시 적으로'close'를 호출 할 필요가 없습니다. – StoryTeller