20
파일 출력 파일 출력의 개요 파일 스트림 오브젝트에 의한 출력 멤버함수에 의한 출력

파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

파일 입․출력

• 파일 입 출력의 개요

• 파일 스트림 오브젝트에 의한 입 출력

• 멤버함수에 의한 입 출력

Page 2: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

파일 스트림 오브젝트에 의한 입출력

클래스 설 명

ifstream ∙ 입력 파일 스트림 클래스

∙ 디스크의 데이터를 읽어 들이는 객체 생성

ofstream ∙ 출력 파일 스트림 클래스

∙ 디스크에 데이터를 파일로 쓰는 객체 생성

fstream ∙ 디스크 입․출력에 모두 관계하는 스트림 클

래스

Page 3: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

출력파일 열기

outFile : 출력위한 스트림 클래스의 객체 이름으로 사용자가 지정

fileName : 출력 파일 이름, char * 형 포인터 사용

ios::out : 파일열기 모드 표시. ofstream의 디폴트(default)는 ios::out 이를 생략 가능

ofstream outFile("fileName", ios::out);

Page 4: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

파일모드 설 명

ios::out 디폴트 모드. 파일에 데이터를 기록한다.

ios::in 기존 파일을 열 경우, 데이터 기록 후 남은 부분은

그대로 둔다.

ios::app 기존 파일에 데이터를 추가

ios::ate 파일의 끝(at end)으로 이동

ios::trunc 기존 파일의 내용을 지우고 데이터를 기록

ios::nocreate 파일이 존재하지 않으면 에러 발생

ios::noreplace 파일이 이미 존재하면 에러 발생

ios::binary 이진 모드로 파일을 개방

Page 5: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

출력파일 열기

ofstream outFile; outFile.open(fileName, ios::out);

파일 닫기

outFile.close();

Page 6: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

#include <iostream.h>

#include <process.h> // exit() 에 필요

#include <fstream.h>

void main()

{

fstream fp; // outFile을 fp로 지정

fp.open("test.dat", ios :: out); //출력 파일 test.dat 생성

if( !fp )

{

cout << “can not open file \n" ;

exit(0);

}

fp << " This test program. \n" ;

fp << " Write data in file. \n" ;

fp.close(); // 파일 닫기

} This test program. Write data in file.

Page 7: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

input your id : 100 input your id : 200 input your id : 0

#include <iostream.h> #include <fstream.h> void main() { int id; fstream fp; fp.open("test2.dat", ios :: out); //출력 파일 test.dat

생성 if( !fp ) { cout << “can not open file \n" ; exit(0); }

while(1) { cout << “input your id : " ; cin >> id ; if ( id == 0 ) break; fp << id << endl ; } }

Page 8: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

입력파일 열기

inFile : 입력위한 스트림 클래스의 객체 이름으로 사용자가 지정

fileName : 입력파일 이름, char* 형 포인터 사용

Ios::in : 파일 모드 표시, 디폴트값으로 생략가능

Ifstream inFile(“fileName”, ios::in);

Page 9: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

파일모드 설 명

ios::in 디폴트 모드. 파일에 데이터를 읽어 드린다.

ios::nocreate 파일이 존재하지 않으면 에러 발생

ios::binary 이진 모드로 파일ㄹ 개방

Page 10: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

입력파일 열기

ifstream inFile; inFile.open(fileName, ios::in);

파일 닫기

inFile.close();

Page 11: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

#include <iostream.h>

#include <fstream.h>

#include <stdlib.h>

void main()

{

int ip_in ;

fstream fp_in;

fp_in.open("test2.dat", ios::in);

if( !fp_in) {

cout << " can not open file. \n";

exit(0);

}

fp_in >> ip_in;

cout << ip_in ;

fp_in >> ip_in;

cout << ip_in;

fp_in.close();

}

100

200

Page 12: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

// 스트림 클래스를 이용한 파일처리

#include <fstream.h>

#include <iomanip.h>

void main()

{

double a, b, c, x=1.5;

char * fileName;

fileName="testFile1.dat";

ofstream fp; // 출력파일 열기

fp.open(fileName);

// 출력파일 스트림 포맷 설정 (iomanip.h 헤더 필요)

fp << setiosflags(ios::fixed)

<<setiosflags(ios::showpoint)<<setprecision(3);

// 파일에 데이터 쓰기

for (int i=1; i<=3; i++)

{

fp<<setw(5)<<x<<setw(10)<<x*x

<<setw(10)<<x*x*x<<endl;

Page 13: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

x++;

}

fp.close(); // 출력파일 닫기

ifstream fp_in; // 입력파일 열기

fp_in.open(fileName);

cout << "입력파일 이름: "<<fileName<<"\n";

// 입력파일 스트림 포맷 설정

cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)

<<setprecision(3);

// 파일로부터 데이터 읽기

for (i=1; i<=3; i++)

{

fp_in>>a>>b>>c;

cout<<setw(5)<<a<<setw(10)<<b

<<setw(10)<<c<<endl;

}

fp_in.close(); // 입력파일 닫기

}

입력파일 이름: testFile1.dat 1.500 2.250 3.375 2.500 6.250 15.625 3.500 12.250 42.875

Page 14: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

멤버함수에 의한 입출력

ofstream outFile("fileName", ios::binary); outFile.write((char *) &objectName, sizeof objectName);

ifstream inFile("fileName", ios::binary); inFile.read((char *) &objectName, sizeof objectName);

read()함수

write()함수

저장객체

Page 15: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

#include <iostream.h>

#include <process.h>

#include <string.h>

#include <fstream.h>

void main()

{

char *month[6]={"jan","feb","mar","apr","may",""};

int n=0;

ofstream fp;

fp.open("test1.dat",ios::out|ios::binary);

if(!fp)

{

cout << "can not openfile. \n";

exit(0);

}

Page 16: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

while(1)

{

if( n > 5 ) break ;

fp.write(month[n], strlen(month[n]));

cout << month[n];

n++;

}

}

janfebmaraprmay

Page 17: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

#include <iostream.h>

#include <process.h>

#include <string.h>

#include <fstream.h>

void main()

{

char buf[5];

int n=0;

memset(buf, 0, 5);

ifstream fp_in;

fp_in.open("test1.dat", ios::in | ios::binary);

if(!fp_in) {

cout << "can not open file. \n" ;

exit(0);

}

Page 18: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

while(1)

{

if( n > 5 ) break ;

fp_in.read(buf, 3);

cout << buf << endl;

n++;

}

fp_in.close();

}

jan

feb mar apr may

Page 19: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

// 이진파일 입출력

#include <fstream.h>

class Point // 클래스 선언

{

public:

Point(double x=0, double y=0): itsX(x), itsY(y) {}

~Point() {}

void PrintPosition() const

{cout<<"("<<itsX<<", "<<itsY<<")"<<endl;}

private:

double itsX;

double itsY;

};

Page 20: 파일 입 출력 - KOCWelearning.kocw.net/contents4/document/lec/2013/... · 파일모드 설 명 ios::out 디폴트 모드. 파일에 데이터를 기록한다. ios::in 기존 파일을

void main()

{

Point P1(3,4), P2(7,8);

cout<<"P1 초기 데이터: ";

P1.PrintPosition(); // P1 객체의 초기 데이터

ofstream outFile("testFile2.dat",ios::binary); //이진file 열기

outFile.write((char *) &P2, sizeof P2); // 데이터 저장

outFile.close(); // 파일 닫기

ifstream inFile("testFile2.dat",ios::binary); // 이진파일 열기

inFile.read((char *) &P1, sizeof P1); // 데이터 읽기

inFile.close(); // 파일 닫기

cout<<"P1 변경된 데이터: ";

P1.PrintPosition(); // P1 객체의 변경된 데이터

}

P1 초기 데이터: (3, 4) P1 변경된 데이터: (7, 8)