[c++] 파일을 쓰기
#include <fstream>
#include <iostream>
using namespace std;

int main() {
    ofstream file("example.txt"); // 파일을 쓰기 모드로 열기

    if (file.is_open()) { // 파일이 성공적으로 열렸는지 확인
        file << "Hello, World!"; // 파일에 내용 쓰기
        file.close(); // 파일 닫기
        cout << "File written successfully." << endl;
    } else {
        cout << "Failed to open the file." << endl;
    }

    return 0;
}

위 코드는 “example.txt” 파일을 열고 “Hello, World!” 라는 내용을 파일에 씁니다. 파일이 성공적으로 쓰여진 후에는 파일을 닫고 작업 완료 메시지를 출력합니다.

이 코드를 실행하면 “example.txt” 파일이 생성되고 그 안에 “Hello, World!”가 쓰여진 것을 확인할 수 있습니다.

참고 문헌: