목적
- 파일 입출력 객체를 이해한다.
문제
코드
#include <iostream>
#include <ctype.h>
#include <fstream>
해설#include <ctype.h>
#include <fstream>
using namespace std;
int main(void)
{
char cBuf;
ifstream fIn("file1.txt", ios::in);
ofstream fOut("file2.txt", ios::out);
if(!fIn)
{
cerr << "file1.txt 파일이 없습니다." << endl;
return -1;
}
while(fIn.get(cBuf))
{
if(isalpha(cBuf) || cBuf == '\n')
{
fOut.put(cBuf);
}
}
fOut.close();
fIn.close();
return 0;
}
- fstream 객체는 파일의 입출력을 담당하는 객체입니다. 객체 생성(인스턴스화)시 생성자에 인수로 파일과 옵션을 넣을수 있습니다.
- fstream 객체는 iostream 객체를 상속 받았기 때문에 iostream 에서 쓰는 맴버 함수 일부를 쓸수 있습니다.
참고
- http://www.winapi.co.kr/clec/cpp4/36-1-4.htm : fstream 객체