读书笔记(C++)——【标准IO库】

//8.标准IO库
//条件状态
//表8 — 2
strm::iostate       //机器相关整形名,定义条件状态
strm::badbit        //strm::iostate类型的值,用于指出被破坏的值
strm::failbit       //strm::iostate类型的值,用于指出失败的IO操作
strm::edfbit        //strm::iostate类型的值,用于指出流已经到达文件结束符
s.eof()             //如果设置了流s的eofbit值,则函数返回true
s.fail()            //如果设置了流s的failbit值,则函数返回true
s.bad()             //如果设置了流s的badbit值,则函数返回true
s.good()            //如果流s处于有效状态,则函数返回true
s.clear()           //将流s中的所有状态都重设为有效状态
s.clear(flag)       //将流s中的某个指定条件状态设置为有效,flag的类型是strm::iostate
s.setstate(flag)    //给流s添加指定条件,flag的类型是strm::iostate
s.rdstate()         //返回流s的当前条件,返回值类型为strm::iostate
//
int main()
{
    int val;
    cin>>val;
    if(cin)
        cout<<val<<endl;
    else
        cout<<"error"<<endl;
    return 0;
}
//
int main()
{
    int val;

    while(cin>>val,!cin.eof())
    {
        if(cin.bad())
            throw runtime_error("IO stream corrupted");
        if(cin.fail())
        {
            cerr<<"bad data,try again";
            cin.clear(iistream::failbit);
            continue;
        }
    }
    return 0;
}
//条件状态的访问
    istream::iostate old_state = cin.rdstate();
    cin,clear();
    process_input();
    cin.clear(old_state);
//多种状态的处理
    is.setstate(ifstream::failbit | ifstream::badbit);
//8.3 输出缓冲区的管理
//输出缓冲区的刷新
    cout<<flush;//flushes the buffer,adda no data;
    cout<<ends;//inserts a null,then fulshes the buffer;
    cout<<endl;//inserts a newline,then flushes the buffer;
//unitbuff操纵符
    cout<<unitbuf<<"first"<<"second"<<nounitbuf;
等价于
    cout<<"first"<<flush<<"second"<<flush;
//8.4.1文件流对象的使用
    ifstream infile(ifile.c_str());//construct an ifstream and bind it ti the file named ifile;
    ofstream outfile(oflie.c_str());

    ifstream infile;
    ofstream outfile;
    infile.open("in");//open file named "in" in the current directory;
    outfile.open("out");//open file named "out" in the current directory;
    //check that the file succeed
    if(!infile)
    {
        cerr<<"error:unable to open input file:"<<infile<<endl;
        return -1;
    }
//将文件流与新文件重新捆绑
    ifstream infile("in");
    infile.close();
    infile.open("next");
//清除文件流的状态
    ifstream input;
    vector<string>::const_iterator it = files.begin();
    while(it != files.end())
    {
        input.open(it->c_str());
        if(!input)
            break;
        while(input>>s)
            process(s);
        input.close();
        input.clear();
        ++it;
    }
//8.4.2文件模式
//表8 - 3 文件模式
in//打开文件做读操作
out//打开文件做写操作
app//在每次写之前找到文件末尾
ate//打开文件后;;立即将文件定位在文件末尾
trunc//打开文件时清空已存在的文件流
binary//以二进制模式进行IO操作

    ofstream outfile1("file1");
效果等同
    ofstream outfile2("file1",ofstream::out | ofstream::trunc);
    //append mode:adds new data at end of exsiting file named "file2";
    ofstream appfile("file2",ofstream::app);


    //open for input and output
    fstream inOut("copyOut",fstream::in | fstream::out)

//模式是文件属性而不是流的属性
    ofstream outfile;
    outfile.open("scratchpad",ofstream::out);
    outfile.close();

    outfile.open("precious",ofstream::app);
    outfile.close();

    //output mode set by default,"out" truncated;
    outfile.open("out");
//打开模式的有效组合
//表 8 - 4 文件模式的组合
out              //先删再写
out | app        //在文件尾写入
out | trunc      //同out
in               //
in | out         //读写并定位于文件开头
in | out | trunc // 读写且删除文件原有数据
//均可添加ate模式
//8.4.3 打开并检查输入文件的程序
ifstream& open_file(ifstream &in,const string &file)
{
    in.close();
    in.clear();
    in.open(file.c_str());
    return in;
}
//8.5 字符串流
//表 8 - 5 stringstream特定的操作
stringstream strm;
stringstream strm(s); //string s;
strm.str()            //返回strm中存储的string类型对象
strm.str(s)           //将string类型的s复制给strm,返回void
//处理每行中每个单词
    string line,word;
    while(getline(cin,line))
    {
        istringstream stream(line);
        while(stream>>word)
        {
            cout<<word<<endl;
        }
    }
//stringstream提供的转换和/或格式化
    int val1 = 512,val2 = 1024;
    ostringstream format_message;
    format_message<<"val1: "<<val1<<endl
                  <<"val2: "<<val2<<endl;
    istringstream input_istring(format_message.str());
    string dump;
    input_istring>>dump>>val1>>dump>>val2;
    cout<<val1<<" "<<val2<<endl;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值