Skip to content

Streams

Easy to locate in a one-demension line, which flows in a single direction.

Kinds of streams

Kinds of streams

(i) text streams

  • readable

  • organized in lines

(ii) binary streams

C++
cout<<"Hello\n";
cerr<<"Byebye\n";

if we do

C++
./a.out > 1 // hello in 1, Byebye in terminal
./a.out 2> 1 // Byebye in 1, hello in termial
./a.out >1 2> 2 // hello in 1, Byebye in 2

int get(), a member function, returns the next character in the stream, EOF if no character left. istream& get(char& ch), a manipulator.

C++
// copy input to output
int ch;
while(ch=cin.get()!=EOF){
  cout.put(ch);
}

use free function instead of object

C++
// recommended
istream& getline(istream& is, string& str, char delim='\n') // delim is the end sign

// not recommended, you need to create an array, 
cin.getline(char *, int size)

ignore(int limit=1, int delim=EOF), a member function, skip over limit number character or until delim. Like skipping until the end of line, or let wrong string to be read, such that program could continue.

int gcount() returns number of characters just read.

Text Only
string buffer;
getline(cin, buffer);
cuot<< "read "<< cin.gcount()<<" characters"

cin.putback(char) pushes a single character into a stream

char peek() examines next character without reading it.

flush forces content in ostream to output.

Manipulators

Manipulators modify the state of the stream.

Manipulator effect type
dec, hex, oct set numberic conversion I,O
endl insert new line and flush O
flush flush stream O
setw(int) set field width I,O
setfill(char) change fill character(like #) I,O
setbase(int) set number base(like 2) I,O
ws skip whitespace I
setprecision(int) set floating point precision O
C++
#include <iomanip>
int n;
cout << "enter number in hexadecimal" << flush; // only takes effect once
cin >> hex >> n; // effexts hold
C++
#include <iostream>
#include <ismanip>
main(){
  cout << setprecision(2) << 1000.243 << endl;
  cout << setw(10) << "OK!";
}

// returns
1e03
          OK!

we can create our own manipulator.

C++
ostream& manip(ostream& out){
  ...
  return out;
}

ostream& tab (ostraam& out){
  return out << '\t';
}

cout << "Hello" << tab << "world"<< endl;

Stream flags

C++
// set amnd reset using manipulators
setiosflags(flags) // set '1'
resetiosflag(flags) // reset '0'

// using `stream` member function
cin.setf(flags)
cout.unsetf(flags)
C++
main(){
  // add two functions
  cout.setf(ios::showpos | ios::scientific);
  cout << 123 << " " << 456.78 <<endl;
  cout << resetiosflags(ios::showpos) << 123;
  return 0;
}

// output
+123 +4.567800e+02
123

clear() returns error stream to GOOD, which is useful for reading failure.

checking status: good(), eof, fail(), bad() return true or false.

C++
int n;
while(cin.good()){// skip if eof or bad
  cin >> n;
  if(cin){ // overload operator bool() cin, cin.put
    cin,ignore(INT_MAX, '\n');
    break;
  }
  if(cin.fail()){
    cin.clear();
    cin,ignore(INT_MAX, '\n');
    cout << "No good, try again!" << flush;
  }
}

File streams

In <fstream>, ifstream, ofstream connects files to streams.

Open modes specify how to create files using flags.

modes purpose
ios::app append
ios::ate position at the end
ios::binary do binary I/O
ios::in open for input
ios::out open for output
C++
#include <iostream>
#include <fstream>
int main(int argc, char *argv[]){
  if(argc !=3){
    cerr << "Usage: copy file1 file2" <<endl;
    exit(1);
  }
  ifstream in(argv[1]);
  if(! in){
    cerr << "Unable to open file " << argv[1];
    exit(2);
  }
  ofstream out(argv[2]);
  if(! out){
    cerr << "Unable to open file " << argv[];
    exit(2);
  } 
}

more stream operations like open(const char *, int flags, int) to open a specified file, close() to close a stream.

C++
ifstream inputS;
inputS.open("somefile", ios::in);
if(!inputS){
  cerr << "Unable to open somefile";
}