c++ Streams

Streams

Advantages of streams

  1. better type safety
  2. Extensible
  3. More object-oriented

Disadvantages

  1. More verbose
  2. Often slower
  3. C标准的输入输出和C++的不能混用。

Stream naming conventions

InputOutputHeader
Genericistreamostream
Fileifstreamofstream
C string(legacy)istrstreamostrstream
C stringistringstreamostringstream

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!";
manipulatoreffecttype
dec, hex, octset numeric conversionI,O
endlinsert newline and flushO
flushflush streamO
setw(int)set field widthI, O
setfill(ch)change fill characterI, O
setbase(int)set number baseO
wsskip whitespaceI
setprecision(int)set floating point precisionO
setiosflags(long)turn on specified flagsI, O
resetiosflags(long)turn off specified flagsI, 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

modepurpose
ios::appappend
ios::ateposition at end of file
ios::binarydo binary I/O
ios::inopen for input
ios::outopen for output
ios::nocreatedon’t create file if not there
ios::noreplacedon’t replace file if present
ios::trunctruncate 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值