美文网首页
13 文件的输出设置

13 文件的输出设置

作者: LuckTime | 来源:发表于2016-06-18 09:57 被阅读18次

    // fill ostream

    include <iostream>

    include <fstream>

    include <iomanip>

    include <string>

    using namespace std;
    class WF
    {
    int i;
    char f;
    public:
    WF(int i,char f) :i(i),f(f){}

    friend ostream& operator<<(ostream& os,WF o)
    {
    os.width(o.i);
    os.fill(o.f);
    return os;
    }
    };
    int main()
    {
    cout.width(10);//fill ten bytes screen.
    cout << 123 << endl;//width only used next time//宽度填充仅对下一次输出有效
    cout << 123 << endl;
    cout.width(1); //fill one byte screen
    cout << 123 << endl;
    cout.width(10);
    cout.fill(''); //free space used of ‘’.often used ..until the end of the program
    cout <<123<<endl;
    cout.width(10);
    cout << 123 << endl;
    cout.precision(8);//set precision //设置精度
    cout << (double)123.0 <<endl;
    //often see output control sign
    cout.setf(ios::left); //left 向左对齐

    cout.setf(ios:: right | ios::showpos);
    cout.setf(ios::scientific | ios::uppercase);
    cout << WF(8,'') << 123 <<endl;
    cout.unsetf(ios::scientific | ios::uppercase);
    cout.unsetf(ios:: right | ios::showpos);
    //====================================
    cout << setw(10) << setfill('~') << left << 123 << endl;
    cout << setw(10) << setfill('+') << right << 123 << endl;
    //================================
    string str;
    str = "hello";
    cout << sizeof(str) <<endl;
    str = "hello,world!";
    cout << sizeof(str) <<endl; //输出结果一样,sizeof只注重类型,不关注内容。。是指针
    //ofstream fout("app.txt",ios::app);//add to the file end//追加在文件后面 //顺序读写方法
    //seekg(位置 int)跳着读写方法
    //getline(cin,str);
    ofstream fout("seek.txt",ios::out | ios::binary);
    fout << "hello,sd0705!" <<endl;
    fout.seekp(0);//first write H
    fout.put('H');
    fout.seekp(7);
    fout <<"S";
    fout << str << endl;
    fout.close();
    /

    ios::right //右对齐
    /========
    ios::dec
    ios::oct
    ios::hex
    /========
    ios::showbase// take prefix output 带着前缀输出
    ios::showpoint //take point output 带着小数点输出
    ios::scientific //science output 科学输入法输出 3.5 e 9
    ios::uppercase // uppercase 大写输出
    ios::showpos// use sign output //使用符号输出
    ios::app //start writing at end of file
    ios::ate //start reading or writing at end of file
    ios::in //open for reading
    ios::trunc //truncate file to zero length if exists
    ios::nocreate //erroe when opening if file does not exist
    ios::noreplace :error when opening for output if file exist
    ios::binary //open file in binary mode
    ios::out //open for writing
    /=======
    //flush :clear out buffer
    endl: insert a '\n' and clear out buffer
    oct:set the output as oct
    dec
    hex
    {
    setw(宽度)
    setfill(填充字符)
    setprecision(精度)
    必须有头文件 <iomanip>
    }
    /
    //cout.left
    //=======================after revision
    /
    char ch;
    int num;
    cout << "cin num and char" << endl;
    cin >> num >> ch;
    cout << WF(num,ch) << 123 <<endl; //must have output //必须有输出
    */

    }

    相关文章

      网友评论

          本文标题:13 文件的输出设置

          本文链接:https://www.haomeiwen.com/subject/wnnvdttx.html