1 includeiostream h ostream setupostream stream stream width10
- Slides: 31
예제 프로그램(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: ”; return stream; } ostream ¬e(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: “; 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 0; } wcha@hansung. ac. kr 7
파일 입출력의 기초(3) q 파일의 access 값 wcha@hansung. ac. kr 10
예제 프로그램(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 : 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){ 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 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 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 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; 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++; in. putback(*p); *p = '