1 includeiostream h ostream setupostream stream stream width10

  • Slides: 31
Download presentation

예제 프로그램(1) #include<iostream. h> ostream &setup(ostream &stream) { stream. width(10); stream. precision(4); stream. fill(‘*’);

예제 프로그램(1) #include<iostream. h> ostream &setup(ostream &stream) { stream. width(10); stream. precision(4); stream. fill(‘*’); return stream; } main() { cout << setup <<123. 123456; return 0; } wcha@hansung. ac. kr Result : 4

예제 프로그램(2) #include<iostream. h> #include<iomanip. h> ostream &atn(ostream &stream) { stream << “Attention: ”;

예제 프로그램(2) #include<iostream. h> #include<iomanip. h> ostream &atn(ostream &stream) { stream << “Attention: ”; return stream; } ostream &note(ostream &stream) { stream << “Please Note: ”; return stream; } main() { cout << atn << “High voltage circuitn”; cout << note << “Turn off all lightn”; Result : return 0; } wcha@hansung. ac. kr 5

입력 조작자(1) #include<iostream. h> #include<string. h> istream &getpass(istream &stream) { cout<<‘a’; cout<<“enter password: “;

입력 조작자(1) #include<iostream. h> #include<string. h> istream &getpass(istream &stream) { cout<<‘a’; cout<<“enter password: “; return stream; } wcha@hansung. ac. kr 6

입력 조작자(2) int main() { char pw[80]; do{ cin>>getpass>>pw; }while(strcmp(pw, “password”)); cout<<“Logon completen”; return

입력 조작자(2) int main() { char pw[80]; do{ cin>>getpass>>pw; }while(strcmp(pw, “password”)); cout<<“Logon completen”; return 0; } wcha@hansung. ac. kr 7

파일 입출력의 기초(3) q 파일의 access 값 wcha@hansung. ac. kr 10

파일 입출력의 기초(3) q 파일의 access 값 wcha@hansung. ac. kr 10

예제 프로그램(1) #include<iostream. h> #include<fstream. h> main() { ofstream fout("test"); if(!fout){ cout <<"cannot open

예제 프로그램(1) #include<iostream. h> #include<fstream. h> main() { ofstream fout("test"); if(!fout){ cout <<"cannot open output file. n"; return 1; } ifstream fin("test"); if(!fin){ cout << "Cannot open input file. n"; return 1; } char str[80]; int i; fin >> str >> i; cout << str << ‘ ’ << i << endl; fout << "Hello!n"; fout << 100<< ‘ ’ << hex << 100 << endl; fin. close(); return 0; Result : } fout. close(); wcha@hansung. ac. kr 12

예제 프로그램(2) #include<iostream. h> #include<fstream. h> main(int argc, char *argv[]) { if(argc !=3){ cout<<"Usage

예제 프로그램(2) #include<iostream. h> #include<fstream. h> main(int argc, char *argv[]) { if(argc !=3){ cout<<"Usage : CONVERT <input><output>n"; return 1; } ifstream fin(argv[1]); ofstream fout(argv[2]); if(!fout){ cout<<"cannot open output file. n"; return 1; } wcha@hansung. ac. kr if(!fin){ cout <<"cannot open input file. n"; return 1; } char ch; fin. unsetf(ios: : skipws): //공백문자무시안됨 while(!fin. eof()){ fin >> ch; if(ch==‘ ’) ch=‘ | ’; fout << ch; } return 0; } 13

예제 프로그램(1) #include<iostream. h> #include<fstream. h> main(int argc, char*argv[]) { char ch; if(argc !=2){

예제 프로그램(1) #include<iostream. h> #include<fstream. h> main(int argc, char*argv[]) { char ch; if(argc !=2){ cout << "Usage : PR<filename>n"; return 1; } ifstream in(argv[1]); if(!in) { cout<<"cannot open filen"; return 1; } while(!in. eof()){ in. get(ch); cout << ch; } return 0; } wcha@hansung. ac. kr 15

예제 프로그램(2) #include<iostream. h> #include<fstream. h> #include<string. h> main() { ofstream out("test"); if(!out){ cout<<"cannot

예제 프로그램(2) #include<iostream. h> #include<fstream. h> #include<string. h> main() { ofstream out("test"); if(!out){ cout<<"cannot open output file. n"; return 1; } double num = 100. 45; char str[]="This is a test"; out. write((char *)&num, sizeof(double)); out. write(str, strlen(str)); out. close(); return 0; } wcha@hansung. ac. kr 16

예제 프로그램(3) #include<iostream. h> #include<fstream. h> main() { ofstream out("test"); if(!out){ cout<<"cannot open output

예제 프로그램(3) #include<iostream. h> #include<fstream. h> main() { ofstream out("test"); if(!out){ cout<<"cannot open output file. n"; return 1; } double nums[4]={ 1. 1, 2. 2, 3. 3, 4. 4 }; out. write((char *)nums, sizeof(nums)); out. close(); ifstream in("test"); if(!in){ cout << "cannot open input file. n"; return 1; } in. read((char *)&nums, sizeof(nums)); int i; for(i=0; i<4; i++) cout << nums[i] <<‘ ’; cout << 'n'; cout << in. gcount()<<"characters readn"; in. close(); return 0; } wcha@hansung. ac. kr Result : 17

2진 입출력 함수(3) #include<iostream. h> #include<fstream. h> main() { char str[80]; cout << "Enter

2진 입출력 함수(3) #include<iostream. h> #include<fstream. h> main() { char str[80]; cout << "Enter your name: "; cin. getline(str, 79); cout << str << 'n'; return 0; Result : } wcha@hansung. ac. kr 20

2진 입출력 함수(4) #include<iostream. h> #include<fstream. h> #include<ctype. h> #include<stdlib. h> main(){ char ch;

2진 입출력 함수(4) #include<iostream. h> #include<fstream. h> #include<ctype. h> #include<stdlib. h> main(){ char ch; ofstream out("test"); if(!out){ cout<<"cannot open output file. n"; return 1; } char str[80], *p; out << 123 << “this is a test” << 23; out << “Hello there!” << 99 << “sdf” << endl; out. close(); ifstream in("test"); if(!in){ cout << "cannot open inpt file. n"; return 1; } wcha@hansung. ac. kr 뒤에 계속 21

2진 입출력 함수(5) do { p=str; ch=in. peek(); if(isdigit(ch)){ while(isdigit(*p = in. get())) p++;

2진 입출력 함수(5) do { p=str; ch=in. peek(); if(isdigit(ch)){ while(isdigit(*p = in. get())) p++; in. putback(*p); *p = ''; cout << “Integer: ” << atoi(str); } else if(isalpha(ch)){ Result : while(isalpha(*p = in. get())) p++; in. putback(*p); *p = ''; cout << “string: ” <<str; } else in. get(); cout << 'n'; } while( !in. eof()); in. close(); return 0; } wcha@hansung. ac. kr 22

임의 접근(1) q 파일의 입출력 포인터의 위치 변경 - istream &seekg(streamoff offset, seek_dir orign)

임의 접근(1) q 파일의 입출력 포인터의 위치 변경 - istream &seekg(streamoff offset, seek_dir orign) - ostream &seekp(streamoff offset, seek_dir orign) w streamoff : offset이 가질 수 있는 최대 유효값을 포함 w seek_dir의 값 - 파일에 관련된 두가지 포인터 w 입력 포인터(get pointer) w 출력 포인터(put pointer) wcha@hansung. ac. kr 23

임의 접근(2) q 파일의 두 포인터에 대한 현재 위치 알아내기 - streampos tellg() -

임의 접근(2) q 파일의 두 포인터에 대한 현재 위치 알아내기 - streampos tellg() - streampos tellp() wcha@hansung. ac. kr 24

예제 프로그램 Result : #include<iostream. h> #include<fstream. h> #include<stdlib. h> main(int argc, char *argv[])

예제 프로그램 Result : #include<iostream. h> #include<fstream. h> #include<stdlib. h> main(int argc, char *argv[]) { char ch; if(argc !=3){ cout <<"Usage : LOCATE<filename><loc>n"; return 1; } in. seekg (atoi(argv[2]), ios : : beg); ifstream in(argv[1]); while(!in. eof()) { if(!in){ cout << "cannot open input file. n"; in. get(ch); cout << ch; } return 1; in. close(); } return 0; } wcha@hansung. ac. kr 25

예제 프로그램 #include<iostream. h> #include<fstream. h> main(int argc, char *argv[]) { char ch; if(argc!=2){

예제 프로그램 #include<iostream. h> #include<fstream. h> main(int argc, char *argv[]) { char ch; if(argc!=2){ cout << "PR: <filename>n"; return 1; } ifstream in(argv[1]); if(!in){ cout<<"cannot open input file. n"; return 1; } wcha@hansung. ac. kr while(!in. eof()){ in. get(ch); if(!in. good() && !in. eof()){ cout<<"I/O error. . terminatingn"; return 1; } cout << ch; } in. close(); return 0; } 28

사용자 자신의 입출력과 파일(1) q 삽입 및 추출 연산자의 중복 - 파일에도 적용 q

사용자 자신의 입출력과 파일(1) q 삽입 및 추출 연산자의 중복 - 파일에도 적용 q 예제 프로그램 #include<iostream. h> #include<fstream. h> class coord{ int x, y; public : coord(int i, int j){ x = i; y = j; } friend ostream &operator << (ostream &stream, coord ob); friend istream &operator >> (istream &stream, coord &ob); }; wcha@hansung. ac. kr 뒤에 계속 29

사용자 자신의 입출력과 파일(2) ostream &operator << (ostream &stream, coord ob) { stream <<

사용자 자신의 입출력과 파일(2) ostream &operator << (ostream &stream, coord ob) { stream << ob. x << ‘ ’<< ob. y << 'n'; return stream; } istream &operator >> (istream &stream, coord &ob) { stream >> ob. x >> ob. y; return stream; } 뒤에 계속 wcha@hansung. ac. kr 30

사용자 자신의 입출력과 파일(3) main() { coord o 1(1, 2), o 2(3, 4); ofstream

사용자 자신의 입출력과 파일(3) main() { coord o 1(1, 2), o 2(3, 4); ofstream out("test"); if(!out){ cout << "cannot open output file. n"; return 1; } out << o 1 << o 2; out. close(); ifstream in("test") if(!in){ cout<<"cannot open input file. n"; return 1; } coord o 3(0, 0), o 4(0, 0); in >> o 3 >> o 4; cout << o 3 << o 4; Result : return 0; } wcha@hansung. ac. kr 31