Streams
Advantages of streams
- better type safety
- Extensible
- More object-oriented
Disadvantages
- More verbose
- Often slower
- C标准的输入输出和C++的不能混用。
Stream naming conventions
Input | Output | Header | |
---|---|---|---|
Generic | istream | ostream | |
File | ifstream | ofstream | |
C string(legacy) | istrstream | ostrstream | |
C string | istringstream | ostringstream |
Stream operations
- Extractors: Read a value from stream, overload the >> operator
- Inserters: Insert a value into a stream, overload the << operator
- Manipulators: Change the stream state
Kinds of streams
-
Text streams
Deal in ASCII text, perform some character translation, include files and character buffers
-
Binary streams
Binary data, no translations
Predefined streams
- cin: standard input
- cout: standard output
- cerr: unbuffered error(debugging) output
- clog: buffered error(debugging) output
#include <iostream>
int i;float f;char c;
char buffer[80];
//Read the next character
cin >> c;
//Read an integer
cin >> i;
//Read a float and a string separater by whitespace
cin >> f >> buffer;
More input operators
// read up to limit characters, or to delim
// Appends a null character to buf
// Does not consume the delimiter
get(char *buf, int limit, char delim = '\n')
// read up to limit characters, or to delim
// Appends a null character to buf
// Does consume the delimiter
getline(char *buf, int limit, char delim = '\n')
//Skip over limit characters or to delimiter
//Skip over delimiter if found
ignore(int limit = 1, int delim = EOF)
// return number of characters just read
int gcount()
cout <<"read "<<cin.gcount()<<" characters"
//push a single character back into the stream
void putback(char)
//examines next character without reading it
char peek()
switch(cin.peek())...
output operators
//print a single character
cout.put('a')
cerr.put('!')
//force output of stream contents
flush()
cout << "Enter a number"
cout.flush();
formatting using manipulators
#include<iomanip>
cin>>hex>>n;
cout<<setprecision(2)<<1000.243<<endl;
cout<<setw(20)<<"OK!";
manipulator | effect | type |
---|---|---|
dec, hex, oct | set numeric conversion | I,O |
endl | insert newline and flush | O |
flush | flush stream | O |
setw(int) | set field width | I, O |
setfill(ch) | change fill character | I, O |
setbase(int) | set number base | O |
ws | skip whitespace | I |
setprecision(int) | set floating point precision | O |
setiosflags(long) | turn on specified flags | I, O |
resetiosflags(long) | turn off specified flags | I, O |
//Working with flags
#include<iostream>
#include<iomanip>
void main(){
cout.setf(ios::showpos|ios::scientific);
cout<<123<<" "<<456.78<<endl;
cout<<resetiosflags(ios::showpos)<<123;
return 0;
}
File streams
ifstream, ofstream connect files to streams
mode | purpose |
---|---|
ios::app | append |
ios::ate | position at end of file |
ios::binary | do binary I/O |
ios::in | open for input |
ios::out | open for output |
ios::nocreate | don’t create file if not there |
ios::noreplace | don’t replace file if present |
ios::trunc | truncate file if present |
# include<iostream>
# include<fstream>
int main(int argc, char *argv[]){
if(argc!= 3){
cerr<<"Usage: copy file1 file2"<< end;
exit(1);
}
ifstream in(argv[1]);
if(!in){
cerr<< "Unable to open file"<< argv[1];
exit(2);
}
}
stream operations
-
open(const char *, int flags, int)
open a specified file:
ifstream inputS; inputS.open("somefile", ios::in); if(!inputS){ cerr<< "Unable to open somefile"; }
-
close()
Close stream