728x90
1. 파일 입출력 기본 개념
C++에서 파일 입출력을 수행하기 위해 아래 클래스들을 사용합니다:
ifstream
: 입력 파일 스트림으로, 파일에서 데이터를 읽어오는 데 사용됩니다.ofstream
: 출력 파일 스트림으로, 데이터를 파일에 기록하는 데 사용됩니다.fstream
: 양방향 스트림으로, 읽기와 쓰기가 모두 가능합니다.
이 클래스들은 <fstream>
헤더 파일에 정의되어 있으며, 사용하기 위해서는 반드시 해당 헤더를 포함해야 합니다.
#include <iostream>
#include <fstream> // 파일 입출력을 위한 헤더 파일
2. 파일 쓰기: ofstream
기본 예제
아래는 텍스트 파일에 데이터를 쓰는 간단한 예제입니다:
#include <iostream>
#include <fstream>
int main() {
std::ofstream outFile("example.txt"); // 출력 파일 생성
if (outFile.is_open()) { // 파일 열기 성공 여부 확인
outFile << "안녕하세요, C++의 파일 입출력입니다!" << std::endl;
outFile << "파일 입출력이 성공적으로 완료되었습니다." << std::endl;
outFile.close(); // 파일 닫기
} else {
std::cerr << "파일을 열 수 없습니다!" << std::endl;
}
return 0;
}
주요 포인트
is_open()
: 파일 열기 성공 여부를 확인합니다.outFile << ...
: 파일에 데이터를 씁니다.close()
: 파일 작업 후 반드시 닫습니다.
추가 팁
- 파일 이름을 사용자 입력으로 받아 동적으로 지정할 수 있습니다.
- 여러 줄 데이터를 반복적으로 쓰기 위해 루프를 활용할 수 있습니다.
std::string fileName;
std::cout << "출력할 파일 이름을 입력하세요: ";
std::cin >> fileName;
std::ofstream outFile(fileName);
3. 파일 읽기: ifstream
기본 예제
다음은 텍스트 파일에서 데이터를 읽어오는 예제입니다:
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream inFile("example.txt"); // 입력 파일 열기
if (inFile.is_open()) {
std::string line;
while (getline(inFile, line)) { // 한 줄씩 읽기
std::cout << line << std::endl; // 읽은 내용을 콘솔에 출력
}
inFile.close(); // 파일 닫기
} else {
std::cerr << "파일을 열 수 없습니다!" << std::endl;
}
return 0;
}
주요 포인트
getline()
: 파일에서 한 줄씩 데이터를 읽어옵니다.std::cout
: 읽은 데이터를 출력합니다.
활용 예제
특정 단어가 포함된 줄만 출력하려면 std::string
의 find
메서드를 사용할 수 있습니다:
if (line.find("C++") != std::string::npos) {
std::cout << line << std::endl;
}
4. 파일 스트림 모드 설정
주요 모드
파일 스트림은 다양한 모드로 열 수 있습니다:
std::ios::app
: 기존 파일 끝에 데이터를 추가합니다.std::ios::binary
: 바이너리 모드로 파일을 엽니다.std::ios::in
: 읽기 전용으로 파일을 엽니다.std::ios::out
: 쓰기 전용으로 파일을 엽니다.std::ios::trunc
: 기존 내용을 삭제하고 새로 작성합니다.
바이너리 파일 열기 예제
#include <fstream>
std::ofstream outFile("example.bin", std::ios::binary | std::ios::app);
5. 에러 처리
파일 작업 시 에러 처리는 매우 중요합니다. 아래는 유용한 에러 처리 방법들입니다:
파일 열기 실패 확인
if (!inFile.is_open()) {
std::cerr << "파일을 열 수 없습니다!" << std::endl;
}
빈 파일 확인
if (inFile.peek() == std::ifstream::traits_type::eof()) {
std::cerr << "파일이 비어 있습니다!" << std::endl;
}
파일 존재 여부 확인 (C++17 이상)
#include <filesystem>
if (!std::filesystem::exists("example.txt")) {
std::cerr << "파일이 존재하지 않습니다!" << std::endl;
}
6. 다양한 데이터 타입 다루기
정수와 실수 쓰기
#include <fstream>
int main() {
std::ofstream outFile("data.txt");
if (outFile.is_open()) {
int age = 30;
double height = 175.5;
outFile << age << std::endl;
outFile << height << std::endl;
outFile.close();
}
return 0;
}
사용자 정의 객체 다루기
#include <iostream>
#include <fstream>
struct Person {
std::string name;
int age;
};
int main() {
std::ofstream outFile("person.txt");
if (outFile.is_open()) {
Person p = {"홍길동", 25};
outFile << p.name << "," << p.age << std::endl;
outFile.close();
}
return 0;
}
7. 실습 문제
문제 1: 여러 줄의 문자열 저장
사용자로부터 여러 줄의 문자열을 입력받아 텍스트 파일에 저장하는 프로그램을 작성하세요.
문제 2: CSV 파일 데이터 읽기
CSV 형식의 데이터를 포함하는 파일에서 각 항목을 추출하여 출력하는 프로그램을 만들어 보세요.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
int main() {
std::ifstream inFile("data.csv");
if (inFile.is_open()) {
std::string line;
while (getline(inFile, line)) {
std::stringstream ss(line);
std::string item;
std::vector<std::string> row;
while (getline(ss, item, ',')) {
row.push_back(item);
}
for (const auto& val : row) {
std::cout << val << " ";
}
std::cout << std::endl;
}
inFile.close();
} else {
std::cerr << "파일을 열 수 없습니다!" << std::endl;
}
return 0;
}
8. 고급 파일 입출력 기술
바이너리 파일 쓰기
#include <iostream>
#include <fstream>
struct Data {
int id;
double value;
};
int main() {
std::ofstream outFile("binary.dat", std::ios::binary);
if (outFile.is_open()) {
Data d = {1, 123.45};
outFile.write(reinterpret_cast<const char*>(&d), sizeof(Data));
outFile.close();
} else {
std::cerr << "파일을 열 수 없습니다!" << std::endl;
}
return 0;
}
바이너리 파일 읽기
#include <iostream>
#include <fstream>
struct Data {
int id;
double value;
};
int main() {
std::ifstream inFile("binary.dat", std::ios::binary);
if (inFile.is_open()) {
Data d;
inFile.read(reinterpret_cast<char*>(&d), sizeof(Data));
std::cout << "ID: " << d.id << ", Value: " << d.value << std::endl;
inFile.close();
} else {
std::cerr << "파일을 열 수 없습니다!" << std::endl;
}
return 0;
}
결론
C++에서의 파일 입출력은 외부 데이터와 상호작용하는 데 매우 유용한 도구입니다. 기본 파일 스트림 사용법에서부터 고급 기술까지 다양한 기능을 이해하고 활용하면, 프로그램이 더 많은 정보를 저장하고 관리할 수 있습니다. 위의 예제와 실습 문제를 통해 파일 입출력을 직접 경험하고 숙달해 보세요. 이는 고급 프로그램 개발의 중요한 기반이 됩니다.
728x90
'프로그래밍 > C++' 카테고리의 다른 글
Makefile과 CMake: 효율적인 C++ 프로젝트 관리와 빌드 시스템 (0) | 2025.02.02 |
---|---|
C++ 멀티스레딩: 스레드 생성, 동기화, 병렬 프로그래밍 활용하기 (0) | 2025.02.02 |
C++ 메모리 관리: 동적 메모리 할당과 스마트 포인터를 활용한 효율적인 자원 관리 (0) | 2025.02.02 |
C++ 고급 기능: 템플릿, 예외 처리, 네임스페이스, 람다 표현식 (0) | 2025.02.02 |
C++ 표준 라이브러리: STL 컨테이너, 반복자, 알고리즘, 함수 객체의 심화 활용 (0) | 2025.02.02 |