Presentation is loading. Please wait.

Presentation is loading. Please wait.

01_ C++ 스타일의 입출력 02_ C 스타일의 입출력

Similar presentations


Presentation on theme: "01_ C++ 스타일의 입출력 02_ C 스타일의 입출력"— Presentation transcript:

1 01_ C++ 스타일의 입출력 02_ C 스타일의 입출력
30장. 입출력 01_ C++ 스타일의 입출력 02_ C 스타일의 입출력

2 cout과 cin 객체 다시 보기(1) cout과 cin 객체의 타입을 알아보자. 스트림의 개념
typedef basic_ostream<char> ostream; typedef basic_istream<char> istream; extern ostream cout; extern istream cin; [30-1]

3 cout과 cin 객체 다시 보기(2) 입출력 클래스의 상속 계층도 [30-2]

4 cout과 cin 객체 다시 보기(3) 입출력과 버퍼링 버퍼에 쌓인 데이터를 강제적으로 방출하는 예 [30-3]
cout << “Hello, World~\n” << flush; cout << “Hello, World~” << endl;

5 입출력 형식 지정하기(1) setf() 함수의 원형 setf() 함수의 사용 예 (p.874 참조)
fmtflags setf( fmtflags f ); fmtflags setf( fmtflags f, fmtflags mask ); cout.setf( ios_base::boolalpha ); cout << true << endl; // 1이 아닌 true가 출력된다. cout.setf( ios_base::hex, ios_base::basefield ); cout << 123 << endl; // 7B를 출력된다. cout.setf( ios_base::showpoint | ios_base::showpos ); cout << << endl; // 이 출력된다.

6 입출력 형식 지정하기(2) setf() 함수로 지정한 형식을 되돌리는 예
ios_base::fmtflags old_flags; old_flags = cout.setf( ios_base::scientific, ios_base::floatfield ); cout << << endl; // e+001을 출력한다. // 보관한 값으로 다시 세팅한다. cout.setf( old_flags, ios_base::floatfield ); cout << << endl; // 12.34를 출력한다. cout.setf( ios_base::showpos ); cout << 333 << endl; // +333 cout.unsetf( ios_base::showpos ); cout << 333 << endl; // 333을 출력한다.

7 입출력 형식 지정하기(3) width() 함수를 사용해서 값이 출력될 공간의 크기를 지정할 수 있다.
precision() 함수를 사용해서 소수점 이하 자리 수를 지정할 수 있다. fill() 함수를 사용해서 빈 칸을 채우는 문자를 지정할 수 있다. cout.width( 10 ); cout << 333 << “, “ << 334 << endl; // _______333,334를 출력한다. cout.setf( ios_base::fixed ); cout.precision( 2 ); cout << << endl; // 3.33을 출력한다. cout.fill( ‘*’ ); cout.width( 10 ); cout << 333 << 두이; // *******333을 출력한다.

8 조종자(Manipulator) 조종자를 사용하면 편하게 입출력 형식을 지정할 수 있다. (p.878 참조)
cout << hex << 333 << endl; // 14d를 출력한다. cout << setw( 10 ) << 333 << endl; // _______333을 출력한다. cout << setiosflags( ios_base::scientific ) << << endl; // e+001 cout << resetiosflags( ios_base::scientific ) << << endl; // 12.34

9 스트림의 상태 스트림의 상태를 알아보기 위한 함수들 현재 cin 객체의 상태가 정상적인지 알아보는 예
bool good() const; // true면, 정상적인 상태 bool eof() const; // true면, 파일이나 입력의 끝에 도달 bool fail() const; // true면, 예상치 못한 입력이 들어오는 등의 문제 발생 bool bad() const; // true면, 잘못된 파일 등의 문제 발생 if ( cin.good() ) { // 정상 상태 } cin.clear(); cin.ignore( numeric_limits<streamsize>::max(), ‘\n’ );

10 파일 입출력 ofstream을 사용해서 파일을 여는 예 (p.881 참조) 파일을 여는데 성공했는지 확인하는 예
ofstream file1( “test1.txt” ); ofstream file2; file2.open( “test2.txt” ); // 출력용으로 기존 파일을 지워 버리고 새 파일을 연다. ofstream file3( “test.txt”, ios_base::out | ios_base::trunc ); // 출력용으로 기존 파일의 뒤에 내용을 추가할 수 있도록 연다. ofstream file4( “test.txt”, ios_base::out | ios_base::app ); ofstream file( “test.txt” ); if ( false == file.is_open() ) { // 파일을 여는데 실패 }

11 C 스타일의 입출력 (1) printf(), scanf()를 사용한 콘솔 입출력 실행 결과
#include <cstdio> int main() { printf("세 개의 숫자를 입력하세요~\n"); // 세 개의 정수를 입력받는다. int a, b, c; scanf("%d%d%d", &a, &b, &c); float ave = float(a + b + c) / 3.0f; printf("입력하신 값 %d, %d, %d의 평균은 %f 입니다.\n",a, b, c, ave); return 0; } [30-5]

12 C 스타일의 입출력 (2) printf()의 사용 printf()와 scanf() 함수에 사용하는 특수 문자 [30-6]
[표 30-5]

13 C 스타일의 입출력 (3) printf()의 기본적인 사용 형식을 지정하는 예 printf("%d ", 65); // 65
printf("%o ", 65); // 101 printf("%X ", 65); // 41 printf("%c ", 65); // A printf("%f ", 12.34); // printf("%e ", 12.34); // e+001 printf("%s 미디어 ", "한빛"); // 한빛 미디어 printf( “%10d”, 333 ); // _______333 출력 printf( “%010d”, 333 ); // 출력 printf( “%+10d”, 333 ); // ______+333 출력 printf( “%-10d”, 333 ); // 333_______ 출력 printf( “%.2f”, ); // 출력


Download ppt "01_ C++ 스타일의 입출력 02_ C 스타일의 입출력"

Similar presentations


Ads by Google