• stream 프로그램과 소스 사이의 흐름 ex) 키보드의 input stream→ 프로그램 ex) 프로그램의 output stream → 디스플레이

  • buffer 입력과 출력을 위한 임시 저장 공간 큰 데이터를 다룰 때 좋다.

  • <ios> 상속 클래스들:

    • <streambuf>
    • <ostream>, <istream> 상속 클래스:
      • <iostream>
        • cin, cout, cerr, clog
        • wcin, wcout, wcerr, wclog
  • cin, cout

    • flushing buffer <ostream>

      • cout<<”hello”<<flush;
      • cout<<endl; //flush buffer, insert \n
    • formatting

      • cout<<hex<<169; //a9
      • oct(cout); cout<<169; //251
      • cout.width(n); //공백 n개 삽입
      • cout.fill(’*’); //공백을 뭘로 할지 정함 cout.width(5); //*****
      • cout.precision(n); //유효숫자 n개, 기본은 6개
      • cout.fixed(); //정수는 유효숫자로 안셈 cout.setf(ios_base::showpoint); //동일 역할
    • manipulators

      Untitled

    • Stream States

      • eofbit: cin이 eof 도달시 1이 됨
      • failbit: cin이 입력 실패시 1이 됨
      • badbit: 기타 에러시(파일 에러 등) 1이 됨
      • good(): bit가 전부 0이면 true
      • eof(): eofbit==1이면 true
      • bad(); “
      • fail(); “
    • while(cin>>input) 가능한 이유: ()오버로드 되어있음; good()반환함

  • File stream

    • <ifstream> ifstream fin; fin.open(”dir”,[ios_base::in]);

      • ios_base::
        • in : 읽기전용
        • out: 쓰기 전용

      fin.is_open() fin.clear(); //stream state bits 초기화 fin.close();

    • <ofstream> ofstream fout; fout.open(”dir”, [ios_base::out]);

    • fout<<”hello”; fin>>var; << , >> 오버로드 되어 있음!

  • Binary Files 0과 1로 전부 저장 ios_base::binary로 열기 fin.read((char*)&p1, sizeof(p1)); fout.write((char*)&p1, sizeof(p1));

  • Random Access

    • seekg() from istream fin.seekg(112): 112번째 바이트로 이동 fin.seekg(112, ios_base::beg); 시작점부터 112바이트
      • ios_base::cur 현재 위치부터
      • ios_base::end 맨 뒤부터
    • seekp() from ostream fin.seekp(112): 112번째 바이트로 이동
  • Incore formatting in-core: 내부에서 stream은 외부와 상호작용하지만, 여기서는 메모리 내에서 흐른다. 문법은 동일함

    • <sstream> (stringstream) sstream ss; string s=”a b c”; ss>>var1>>var2>>var3; //a,b,c 들어감