[c++] 파일 및 디렉토리 분할하기
파일을 생성하는 방법은 다음과 같습니다.
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream file;
file.open("example.txt");
file << "This is an example file.";
file.close();
return 0;
}
위 예시는 파일을 생성하고 “This is an example file.”이라는 내용을 파일에 씁니다.
다음으로, 파일을 삭제하는 방법은 아래와 같습니다.
#include <iostream>
using namespace std;
int main() {
if (remove("example.txt") != 0)
cout << "Error deleting file";
else
cout << "File successfully deleted";
return 0;
}
이제 디렉토리를 생성하고 삭제하는 방법을 살펴보겠습니다.
디렉토리를 생성하는 방법은 다음과 같습니다.
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
using namespace std;
int main() {
fs::create_directory("my_directory");
return 0;
}
이제 디렉토리를 삭제하는 방법을 살펴보겠습니다.
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
using namespace std;
int main() {
if (fs::remove("my_directory"))
cout << "Directory successfully deleted";
else
cout << "Error deleting directory";
return 0;
}
이와 같이 C++을 사용하여 파일 및 디렉토리를 생성하고 삭제하는 방법을 알아보았습니다. 이러한 파일 시스템 조작 기능을 활용하여 프로그램을 개발할 수 있습니다.
더 많은 정보는 아래 레퍼런스를 참조하시기 바랍니다.