ProgrammingLang/c++

[C++ 개발자되기] 17. std::cout 출력 포맷 변경 iomanip

jinkwon.kim 2020. 3. 18. 10:50
728x90
반응형

>>[C++ 관련 모든 글 보기]

1. std::cout 출력 포맷 변경 방법

  c++ 에서는 iomanip에서 제공하는 함수를 사용하여 포맷을 변경 할 수 있습니니다. 

iomanip는 Input Output Manipmanipulation이라르 뜻 입니다. 

 

2. 필요 헤더 

#include <iomanip>

 

3. 사용법

  - std::cout 에 포맷을 먼저 세팅 합니다. 그 후 표현할 정보를 입력 합니다.

  - 다음 코드에서 std::hex 로 표현 할 포맷을 세팅 합니다.

std::cout << std::hex << 100 << std::endl;

  1) 특정 format 설정 (std::setiosflags)

    - 한번 설정한 format은 설정을 제거 하지  않는한 유지 됩니다. 

    - setiosflags에 인잘 값은 꼭 std::ios에 있는 값을 써야 합니다. 그렇지 않으면 에러 납니다

      그외에 사용시에는 std:: 에있는 값을 써도 됩니다.

    - 에러 정보

/root/cpp_test/iomanip.cpp: In function ‘int main()’:
/root/cpp_test/iomanip.cpp:25:57: error: no match for ‘operator|’ (operand types are ‘const fmtflags {aka const std::_Ios_Fmtflags}’ and ‘std::ios_base&(std::ios_base&)’)
     std::cout << std::resetiosflags (std::ios::showbase | std::uppercase);

 

    - 예제 코드 

 /*
  * 1. format을 세팅 하는 방법
  */
 // 1.1 std::setiosflags 를 사용
 std::cout << "------------1. format 세팅 방법 ---------------" << std::endl;
 std::cout << std::setiosflags (std::ios::showbase | std::ios::uppercase);
 std::cout << "setformat     : " << std::hex << num << std::endl;

  2) 특정 format 해제 (std::resetiosflags)

    - 예제 코드 

 /*
  * 2. 특정 format을 해제 하는 방법
  */
 std::cout << "------------2. 특정 format 해제 방법 ----------" << std::endl;
 // 2.1  std::setiosflags 를 사용
 std::cout << std::resetiosflags (std::ios::showbase | std::ios::uppercase);
 std::cout << "unsetformat   : " << std::dec << num << std::endl;

4. Sample 코드

  - 코드 정보 

#include <iostream> // std::cout, std::hex, std::endl
#include <iomanip>  // std::setiosflags

int main()
{
    int num = 10;
    float fnum= 10.001;
    int lhex = 0x1a;
    int uhex = 0x1A;
    double f =3.14159;


    /*
     * 1. format을 세팅 하는 방법
     */
    // 1.1 std::setiosflags 를 사용
    std::cout << "------------1. format 세팅 방법 ---------------" << std::endl;
    std::cout << std::setiosflags (std::ios::showbase | std::ios::uppercase);
    std::cout << "setformat     : " << std::hex << num << std::endl;
    /*
     * 2. 특정 format을 해제 하는 방법
     */
    std::cout << "------------2. 특정 format 해제 방법 ----------" << std::endl;
    // 2.1  std::setiosflags 를 사용
    std::cout << std::resetiosflags (std::ios::showbase | std::ios::uppercase);
    std::cout << "unsetformat   : " << std::dec << num << std::endl;

    /*
     * 3. 현재 cout 상태를 저장
     */
    std::cout << "------------3. 현재 cout 상태를 저장 ----------" << std::endl;
    // 3.1 현재의cout format을 저장 한다.
    std::ios  state(NULL);
    state.copyfmt(std::cout);
    
    /*
     * 4. sample code
     */
    std::cout << "------------4. smaple code --------------------" << std::endl;
    // 10칸 확보, 우측 정렬
    std::cout << "right         : " << std::setw(10) << std::right << "test" << std::endl;

    // 10칸 화보, 좌측정렬
    std::cout << "left          : " << std::setw(10) << std::left << "test" << std::endl;

    // 빈칸 채우기
    std::cout << "set fill      : " << std::setfill ('x') << std::setw (10) << std::right << "77" << std::endl;

    // decimal
    std::cout << "dec to dec    : " << std::dec << num << std::endl;

    // decimal을 Hexa로 표기
    std::cout << "dec to Hex    : " << std::hex << num << std::endl;

    // decimal을 octal로 표기
    std::cout << "dec to oct    : " << std::oct << num << std::endl;

    // hexa 표시 
    std::cout << "hex to hex    : " << std::hex << lhex << std::endl;

    // Hex를 대문짜로 표시
    std::cout << "uppercase hex : " << std::uppercase << lhex << std::endl;

    // 접두 문자를 표시 합니다. hex일경우 0x 
    std::cout << "base hex      : " << std::showbase << lhex << std::endl;

    // 양수를 +로 표기
    std::cout << "pos hex       : " << std::showpos << fnum << std::endl;

    // 끝애0을 표시하여 정확도를 
    std::cout << "point hex     : " << std::showpoint << fnum << std::endl;

    // 정확도 높이기 
    std::cout << std::resetiosflags(std::ios::showpos);
    std::cout << "precision(5)  : " << std::setprecision(5) << f << std::endl;
    std::cout << "precision(9)  : " << std::setprecision(9) << f << std::endl;
    std::cout << "precision(fix): " << std::fixed << f << std::endl;;
    std::cout << "precision(5)  : " << std::setprecision(5) << f << std::endl;
    std::cout << "precision(9)  : " << std::setprecision(9) << f << std::endl;

    std::cout << "------------5. 현재 cout 상태를 복구 ----------" << std::endl;
    // 5. cout 상태 복구 방법, 다음1, 2 하나 선택
    // 5.1. 저장했던 cout 상태 복구
    std::cout.copyfmt(state);

    // 5.2. cout 상태 초기화
    std::cout.copyfmt(std::ios(NULL));
    
    return 0;
}

  - 코드 실행 결과 

>>[C++ 관련 모든 글 보기]

 

* 참조 싸이트 

http://www.cplusplus.com/reference/iomanip/

https://m.blog.naver.com/PostView.nhn?blogId=start3535&logNo=30146765701&proxyReferer=https%3A%2F%2Fwww.google.com%2F

 

728x90
반응형