25
C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming C++ Programming C++ C++ 스타일의 스타일의 입출력 입출력 C++ C++ 스타일의 스타일의 입출력 입출력 Seo Seo, Doo , Doo-ok ok [email protected] [email protected] http://www.Clickseo.com http://www.Clickseo.com

C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

C++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ ProgrammingC++ Programming

C++ C++ 스타일의스타일의 입출력입출력C++ C++ 스타일의스타일의 입출력입출력

SeoSeo, Doo, Doo--okok

[email protected]@gmail.comhttp://www.Clickseo.comhttp://www.Clickseo.com

Page 2: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

목목 차차

C C 스타일의스타일의 입출력입출력

C++C++ 스타일의스타일의 입출력입출력 C++ C++ 스타일의스타일의 입출력입출력

2

Page 3: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

C C 스타일의스타일의 입출력입출력

C 스타일의 입출력

#include <cstdio>

int main(){{

int a, b, c;

printf("세개의정수입력 : ");printf( 세개의정수입력 : );scanf("%d %d %d", &a, &b, &c);

int tot = a + b + c;double ave = tot / 3.0;

printf("총점 : %8d ₩n", tot);i tf("평균 %8 2f ₩ " )printf("평균 : %8.2f ₩n", ave);

return 0;}

3

}

Page 4: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

C++ C++ 스타일의스타일의 입출력입출력

C C 스타일의스타일의 입출력입출력

C++ C++ 스타일의스타일의 입출력입출력

입출력 형식 지정입출력 형식 지정

파일 입출력

4

Page 5: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

C++ C++ 스타일의스타일의 입출력입출력

C++ 스타일의 입출력

#include <iostream>

using std::cin;using std::cout;g ;using std::endl;

int main(){{

int a, b, c;

cout << "세 개의 정수 입력 : ";cin >> a >> b >> c;;

int tot = a + b + c;double ave = tot / 3.0;

cout << "총점 : " << tot << endl;cout << "평균 : " << ave << endl;

return 0;

5

;}

Page 6: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

C++ C++ 스타일의스타일의 입출력입출력 (cont(cont’’d)d)

입출력 클래스의 상속 계층도

ios_base입출력 형식(Format)과

관련된 기능을 제공하는 클래스

basic_istream과

basic_ios basic_streambufbasic_ostream에서

사용하는 기본 기능을

제공하는 클래스 스트림 내부적으로 사용하는

메 리 공간을 관리하는 클래스메모리 공간을 관리하는 클래스

basic_istream basic_ostream

cin 객체의 타입 cout 객체의 타입

6

Page 7: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

C++ C++ 스타일의스타일의 입출력입출력 (cont(cont’’d)d)

cin과 cout 객체

스트림의 끝을 콘솔 창과 연결해 놓은 것

#include <iostream>

extern istream &cin;

extern ostream &cout;

typedef basic_ostream<char> ostream;

typedef basic_istream<char> istream;

7

Page 8: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정

setf 함수

입출력 형식 지정 : ios_basic 클래스의 setf 함수를 사용

setf 함수는 다음과 같이 다중 정의(Overloading)된 두 가지 버전

fmtflags setf( fmtflags f);

fmtflags setf(fmtflags f, fmtflags mask);

8

Page 9: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

setf 함수 (cont’d)

인자가 하나인 tf 함수에 사용할 수 있는 값 인자가 하나인 setf 함수에 사용할 수 있는 값

값 의 미

showbase 진법을 나타내는 기호를 함께 출력

showpoint 실수를 출력할 때 항상 소수점을 출력

showpos 양수의 경우에도 + 기호를 붙여서 출력

uppercase실수를 과학적 표기법으로 출력할 때 문자 ‘E’나

uppercase정수를 출력할 때 사용하는 영문자들을 대문자로 출력

boolalpha bool 타입의 값을 1, 0이 아닌 true, false 형태로 출력하거나 입력

9

Page 10: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : 인자가 하나인 setf 함수

#include <iostream>#include <iostream>

using std::cout;using std::endl;

using std::ios_base;

int main(){

cout.setf(ios_base::showpos);t << 10 << dlcout << 10 << endl;

cout << -10 << endl;

cout.setf(ios_base::boolalpha);cout << true << endl;cout << false << endl;

return 0;

10

return 0;}

Page 11: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : unsetf 함수

#include <iostream>#include <iostream>

using std::cout;using std::endl;

using std::ios_base;

int main(){

cout.setf(ios_base::boolalpha);t << t << dlcout << true << endl;

cout << false << endl;

cout.unsetf(ios_base::boolalpha);cout << true << endl;cout << false << endl;

return 0;

11

return 0;}

Page 12: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

setf 함수 (cont’d)

인자가 둘인 tf 함수의 첫 번째 인자로 사용할 수 있는 값 인자가 둘인 setf 함수의 첫 번째 인자로 사용할 수 있는 값

값 의 미

dec 정수를 10진수로 출력하거나 입력

hex 정수를 16진수로 출력하거나 입력

oct 정수를 8진수로 출력하거나 입력

internal값을 오른쪽으로 정렬해서 출력

하지만 부호(+, -) 혹은 진법을 나타내는 기호(0x, 0)는 왼쪽 정렬 출력

left 값을 왼쪽으로 정렬해서 출력

i ht 값을 오른쪽으로 정렬해서 출력right 값을 오른쪽으로 정렬해서 출력

fixed 실수를 항상 10.12345와 같은 방식으로 출력

scientific 실수를 항상 1 23E+006과 같은 방식으로 출력

12

scientific 실수를 항상 1.23E+006과 같은 방식으로 출력

Page 13: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

setf 함수 (cont’d)

인자가 둘인 tf 함수의 두 번째 인자로 사용할 수 있는 값 인자가 둘인 setf 함수의 두 번째 인자로 사용할 수 있는 값

값 의 미

adjustfield(internal | left | right)와 같은 값이다.

internal과 left와 right를 비트 단위 OR 연산한 것이다.

basefield (dec | hex | oct)와 같은 값이다.

floatfield (fixed | scientific)과 같은 값이다.

13

Page 14: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : 인자가 둘인 setf 함수

#i l d <i t >#include <iostream>

using std::cout;using std::endl;using std::endl;

using std::ios_base;

int main(){

cout setf(ios base::hex ios base::basefield);cout.setf(ios_base::hex, ios_base::basefield);cout << 10 << endl;

cout.setf(ios base::oct, ios base::basefield);( _ , _ );cout << 10 << endl;

return 0;

14

}

Page 15: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : 그 밖의 함수들...

#include <iostream>#include <iostream>

using std::cout;using std::endl;

using std::ios_base;

int main(){

cout << 123 << endl;

cout.width(10);cout << 123 << endl;

cout.fill('0');cout.width(10);cout << 123 << endl;

15

return 0;}

Page 16: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

조종자 (Manipulator)

tf 함수나 기타 함수들을 쉽게 호출하는 방법 setf 함수나 기타 함수들을 쉽게 호출하는 방법

boolalpha showbase showpoint showpos uppercase

noboolalpha noshowbase noshowpoint noshowpos nouppercase

dec hex oct fixed scientific

internal left right

16

Page 17: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : 조정자

#i l d <i t >#include <iostream>

using std::cout;using std::endl;using std::endl;

using std::boolalpha;using std::noboolalpha;;

int main(){{

cout << boolalpha << true << endl;cout << false << endl;

cout << noboolalpha << true << endl;cout << false << endl;

17

return 0;}

Page 18: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

<iomanip> 헤더 파일

인자를 받을 수 있는 조종자를 몇 개 더 정의 인자를 받을 수 있는 조종자를 몇 개 더 정의

값 의 미

setiosflags setf 함수를 사용하는 것과 동일한 효과가 있다.

resetiosflags setf 함수를 통해서 지정한 옵션을 제거하는 효과가 있다.

setfill fill 함수를 호출한 효과가 있다.

setprecision precision 함수를 호출한 효과가 있다.

setw width 함수를 호출한 효과가 있다.

setbase 8, 10, 16 중에 한 값을 넣어서 출력하는 진법을 바꿀 수 있다.

18

Page 19: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : 조정자와 iomanip

#include <iostream>#include <iostream>#include <iomanip>

using std::cout;using std::endl;

using std::hex;using std::dec;using std::oct;

using std::setbase;

int main(){

cout << hex << 10 << endl;cout << dec << 10 << endl;cout << oct << 10 << endl;

cout << setbase(16) << 10 << endl;cout << setbase(16) << 10 << endl;cout << setbase(10) << 10 << endl;cout << setbase(8) << 10 << endl;

return 0;

19

}

Page 20: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : 조정자와 iomanip

#include <iostream>#include <iostream>#include <iomanip>

using std::cout;using std::endl;using std::endl;

using std::left;using std::right;

using std::setw;

int main()int main(){

int num = 12345;

cout << setw(10) << num << endl;cout << setw(10) << num << endl;cout << left << setw(10) << num << endl;cout << right << setw(10) << num << endl;

return 0;

20

return 0;}

Page 21: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

파일파일 입출력입출력

ifstream과 ofstream 클래스의 상속 계층도

basic_istream basic_ostream

basic_ifstream basic_ofstream

class basic_ofstream : public basic_ostreamclass basic_ofstream : public basic_ostream

typedef basic_ofstream<char> ofstream;typedef basic_ifstream<char> ifstream;

21

Page 22: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

파일파일 입출력입출력 (cont(cont’’d)d)

파일 개방

수 수 생성자나 open 멤버 함수를 사용할 수 잇다.

• 파일 이름만 제공하면 출력, 혹은 입력용으로 파일을 생성한다.

ofstream file1(“test.txt”);

ofstream file2;;file.open(“copy.txt”);

22

Page 23: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

파일파일 입출력입출력 (cont(cont’’d)d)

파일 개방 (cont’d)

스트림 열기 옵션 스트림 열기 옵션

값 의 미

app 파일의 끝에 자료를 추가하기 위한 용도로 연다.

ate 파일을 열고 파일의 끝으로 이동한다.

binary 텍스트(text)가 아닌 바이너리(binary)로 입출력을 한다.

in 파일에서 값을 읽기 위한 용도로 연다.

out 파일에 값을 쓰기 위한 용도로 연다.

truc 기존 파일이 있다면 지워버리고 새 파일을 연다.

23

Page 24: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

입출력입출력 형식형식 지정지정 (cont(cont’’d)d)

프로그램 예제 : 파일 개방

#include <iostream>#include <iostream>#include <fstream>

using std::cout;using std::endl;

using std::ofstream;using std::ifstream;

int main()int main(){

ofstream fout("test.txt");

if(!fout)( ){

cout << "test.txt 파일 개방 실패!!!" << endl;return -1;

}

fout << "Hi~ " << endl;fout << "Clickseo!!!" << endl;

fout.close();

24

();

return 0;}

Page 25: C++ProgrammingC++ Programming+_Programming_Stre… · 조종자(Manipulator) setf 함수나기타함수들을쉽게호출하는방법 boolalpha showbase showpoint showpos uppercase

참고문헌참고문헌[1] 윤성우, “열혈강의 C++ 프로그래밍”, 프리렉, 2007.

[2] 이현창, “뇌를 자극하는 C++ 프로그래밍”, 한빛미디어, 2008.

[3] H M D it l P J D it l “C HOW TO PROGRAM 6th Editi ” P ti H ll 2009[3] H.M. Deitel, P. J. Deitel, “C++ HOW TO PROGRAM : 6th Edition”, Prentice Hall, 2009.

[4] Wikipedie, http://www.wikipedia.org/.

이 강의자료는 저작권법에 따라 보호받는 저작물이므로 무단 전제와 무단 복제를 금지하며,

내용의 전부 또는 일부를 이용하려면 반드시 저작권자의 서면 동의를 받아야 합니다.

C i ht © Cli k All i ht d

25

Copyright © Clickseo.com. All rights reserved.