IOStream Manipulators

本文通过一系列示例介绍了C++中标准IO流的使用方法,包括基本的格式化输出、文件操作以及字符串流的使用技巧。此外,还探讨了如何设置输出精度、宽度等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;

int main()
{
    bool b1 = true;
    cout << boolalpha << b1 << " " << noboolalpha << b1 << endl;

    int i1 = 123;
    cout << dec << i1 << " " << hex << i1 << " " << oct << i1 << endl;
    cout << dec << showbase << i1 << " * " << dec << noshowbase << i1 << " # ";
    cout << hex << showbase << i1 << " * " << hex << noshowbase << i1 << " # ";
    cout << oct << showbase << i1 << " * " << oct << noshowbase << i1 << " #/n";

    double d1, d2, d3;
    d1 = 3.1415926534;
    d2 = 2010.0;
    d3 = 1.0e-10;
    cout.precision(5);
    cout << d1 << "/t" << d2 << "/t" << d3 << endl;
    cout << fixed << d1 << "/t" << d2 << "/t" << d3 << endl;
    cout << scientific << d1 << "/t" << d2 << "/t" << d3 << endl;

   
    ofstream outfile1("test1.txt");
    for (int n = 0; n < 100; ++n)
        outfile1 << n << flush;
    outfile1.close();

    int i2 = -77;
    cout.width(6); cout << dec << internal << i2 << endl;
    cout.width(6); cout << left << i2 << endl;
    cout.width(6); cout << right << i2 << endl;

    double d4, d5, d6;
    d4 = 30.0;
    d5 = 10000.0;
    d6 = 3.1416;
    cout.precision(5);
    cout.unsetf(ios::scientific);
    cout << noshowpoint << d4 << '/t' << d5 << '/t' << d6 << endl;
    cout << showpoint << d4 << '/t' << d5 << '/t' << d6 << endl;

    int i3, i4, i5;
    i3 = 1;
    i4 = 0;
    i5 = -1;
    cout << noshowpos << i3 << '/t' << i4 << '/t' << i5 << endl;
    cout << showpos << i3 << '/t' << i4 << '/t' << i5 << endl;
   
    char ch1, ch2, ch3;
    istringstream iss1(" 123");
    iss1 >> noskipws >> ch1 >> ch2 >> ch3;
    cout << ch1 << ch2 << ch3 << endl;
    iss1.seekg(0);
    iss1 >> skipws >> ch1 >> ch2 >> ch3;
    cout << ch1 << ch2 << ch3 << endl;

    ofstream outfile2("test2.txt");
    outfile2 << nounitbuf << "Test " << "file" << endl;
   
    cout << showbase << hex << nouppercase << 77 << endl << uppercase << 77 << endl;

    cout << hex << setiosflags(ios::showbase) << 100 << endl;
    cout << resetiosflags(ios::showbase) << 100 << endl;

    cout << setbase(16) << 100 << endl << setbase(8) << 32 << endl;
    cout << setbase(10) << resetiosflags(ios::showpos);
    cout << setfill('x') << setw(10) << 77 << endl;

    cout << hex << setiosflags(ios::showbase | ios::uppercase) << 100 << endl;

    double d7 = 3.14159;
    cout << setprecision(5) << d7 << endl;
    cout << setprecision(9) << d7 << endl;
    cout << fixed;
    cout << setprecision(5) << d7 << endl;
    cout << setprecision(9) << d7 << endl;

    ofstream outfile3("test3.txt");
    outfile3 << unitbuf << "Test " << "file" << endl;
    outfile3.close();
   
    char a[10], b[10];
    istringstream iss2("one /n /t two");
    iss2 >> noskipws >> a >> ws >> b;
    cout << a << "," << b << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值