stream?

파일이나 콘솔에 입출력을 직접 하지 않고, stream을 통해 동작한다.

기본 입출력stream : stdin, stdout

c언어 입출력 함수 전송 시점(기본)
출력 int putchar(int c);
int fputc(int c, FILE* stream);

int puts(const char* s);
int fputs(const char* s, FILE* stream);
함수가 반환되는 시점
입력 int getchar();
int fgetc(FILE* stream);

char* gets(char* s);
char* fgets(char* s, int n, FILE* stream);   //n: null포함길이
                                                                     줄바꿈 포함됨.
키보드 엔터 후

 

 

stringstream

string 입출력 스트림 클래스.

#include <sstream> 사용

 

입력으로 사용

스트림으로부터 정보를 빼와서 맞는 자료형의 변수에 저장. [공백, '/n' ] 제외

int에 빼올 시, 정수형이 아닌 문자는 실패함.

== istringstream

int num;
string str = "11D2S#10S";
stringstream is;
is.str(str);                //string 등록
while (is >> num) 		//분리 시도
    cout << num << endl;        //11 출력
cout << is.str() << endl;   	//11D2S#10S 출력

while(getline(is, item, '#'))	//특정 문자 기준으로 분리
{
    elems.push_back(item);	//"11D2S" 와 "10S" 저장
}

is.str("");			//빈 문자열로 초기화

 

출력으로 사용

스트림에 정보를 넣음. 문자열 조립(저장)

==ostringstream

    stringstream os;
    string s1 = "abc", s2 = "def";
    int i1 = 111;
    double d1 = 2.222;
    os << s1 << ", " << i1 << ", " << s2 << ", " << d1;     // 스트림에 문자열 조합.
    cout << os.str();       // 문자열을 출력. "abc, 111, def, 2.222"

'c, c++ 리마인드' 카테고리의 다른 글

ANSI, UNICODE, UTF-x  (0) 2023.09.05
c++ 데이터 파일 입출력  (0) 2023.08.27
c++ cast  (0) 2023.08.26
struct, class 크기 결정  (0) 2023.08.25
Big O  (0) 2023.08.24

+ Recent posts