[c++] 파일 및 디렉토리 복구하기
파일 및 디렉토리를 복구하는 과정은 중요하고 매우 유용합니다. 이 글에서는 C++를 사용하여 손상된 파일이나 삭제된 디렉토리를 복구하는 방법을 살펴보겠습니다.
목차
파일 복구
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream inputFile("deleted_file.txt", std::ios::binary);
std::ofstream outputFile("recovered_file.txt", std::ios::binary);
outputFile << inputFile.rdbuf();
inputFile.close();
outputFile.close();
std::cout << "파일 복구가 완료되었습니다." << std::endl;
return 0;
}
위의 코드는 deleted_file.txt
라는 파일을 열고 recovered_file.txt
에 복사하는 간단한 예시입니다.
디렉토리 복구
디렉토리를 복구하기 위해서는 운영 체제의 파일 시스템을 이용할 수 있습니다. 예를 들어, Windows 운영 체제에서는 Windows.h
라이브러리의 CreateDirectory
함수를 사용하여 디렉토리를 생성할 수 있습니다.
#include <iostream>
#include <Windows.h>
int main() {
if (CreateDirectory("recovered_directory", NULL)) {
std::cout << "디렉토리 복구가 완료되었습니다." << std::endl;
}
else {
std::cout << "디렉토리 복구에 실패했습니다." << std::endl;
}
return 0;
}
위의 코드는 recovered_directory
라는 디렉토리를 생성하는 간단한 예시입니다.
결론
파일 및 디렉토리 복구는 중요한 작업입니다. 이 글에서는 C++을 사용하여 파일 및 디렉토리를 복구하는 간단한 예시를 살펴보았습니다. 데이터 복구에는 항상 주의가 필요하며, 복구 작업을 수행하기 전에 백업을 생성하는 것이 좋습니다.
참고 자료
- C++ 파일 입출력: cplusplus.com
- Windows.h 라이브러리: Microsoft Docs