文件重定向,getline()获取一样,屏幕输出流,格式控制符dec,oct,hex,精度控制setprecision(int num),设置填充,cout.width和file(字符),进制输入



1.window下的命令重定向输出到文件中

2.将内容输入到某个文件中的方式:命令<1.txt (使用1.txt中的命令)

3.读取文件中的名,然后将命令读取最后输出到文件中。命令<1.txt>2.txt   这一句的作用就是将执行的命令输入到2.txt中。

4.文件重定向案例1

#include <iostream>

using namespace std;

 

void main()

{

    char str[30] = { 0 };

    cin >> str;

    cout << str;

    system(str);

    //输出错误结果

    cerr << "enter for you";

    cin.get();

    cin.get();

}

5.getline()获取一样

#include <iostream>

#include <stdlib.h>

 

using namespace std;

void main1()

{

    char str[10] = { 0 };

    //作用是获取一行

    cin.getline(str, 10);//限定长度

 

    cout << str;

    system("pause");

    //比如输入:asdad

    //输出结果:asdad

}

 

//cout.put(ch):输出一个字符,cin.get(ch);获得一个字符

void  main()

{

    char ch = 0;

    while (ch != '\t')//复合表达式

    {

        cin.get(ch);//等价于ch=cin.get

        cin.get();

        cout.put(ch); //输出一个字符

    }

}

6.屏幕输出流

A:cout.write():控制输出多大长度的字符串

#include<iostream>

#include <iomanip>//控制输出流

 

using namespace std;

void main()

{

    cout.put('A').put('B').put('C').put('\n');

    char  str[] = "123456789abcdefg";

 

    //通过write输出指定长度的字符串,不包含\0

    cout.write(str,10);

   

    cin.get();

}

输出结果:

B:格式控制符:dec,oct,hex

void main()

{

    //dec,oct,hex都是各式控制符

    int num = 01070;

    cout << num << endl;//默认十进制

 

    cout << hex;//十六进制强制标识,endl结束不了

    cout << num << "  "<< num << "\n" << endl;

    cout << oct;//八进制强制标识,endl结束不了

    cout << num << "  " << num << "\n";

    cout << dec;//十进制强制标识

    cout << num << endl; //默认十进制

    cout << num << endl; //默认十进制

 

    cin.get();

}

运行结果:

C:精度控制setprecision(intnum)

#include<iostream>

#include <iomanip>//控制输出流

 

using namespace std;

void main()

{

    double db = 1.98123178387127838718732;

    cout << db << endl;//这种方式输出小数点后面6

    cout << setprecision(25) << db; //小数点显示精确度

 

    cin.get();

}

输出结果:

D:设置填充,cout.widthfile(字符)

#include<iostream>

#include <iomanip>//控制输出流

 

using namespace std;

void main()

{

    cout.width(40);//设定显示的宽度

    cout.fill('&');//填充字符

    cout << "hello world" << endl;

    cin.get();

}

运行结果:

E:设置左右填充

#include<iostream>

#include <iomanip>//控制输出流

 

using namespace std;

void main()

{

    //字符串输出

    cout.width(40);//设定显示的宽度

    cout.fill('&');//填充字符

    cout.setf(ios::left);//输出的内容左对齐

    cout << "hello world" << endl;

 

    //设定显示的宽度,如果实际长度查过了helloworld,按照实际长度输出

    cout.width(30);

    cout.fill('*');//填充字符

    cout.setf(ios::right,ios::left);

 

    cout << "hello world" << endl;

    cin.get();

}

F:进制输入输出控制,ios::basefield

#include<iostream>

#include <iomanip>//控制输出流

 

using namespace std;

void main()

{

    int num1;

    cin.setf(ios::hex, ios::basefield);//设置输入为十六进制

    cin >> num1;

    cout.setf(ios::hex, ios::basefield);//设置十六进制

    cout << num1;

    int num2;

    cin.setf(ios::dec, ios::basefield);//设置输入为十进制

    cin >> num2;

    cout.setf(ios::dec, ios::basefield);

    cout << num2;

 

    int num3;

    cin.setf(ios::oct, ios::basefield);//设置输入为8进制

    cin >> num3;

    cout.setf(ios::oct, ios::basefield);

    cout << num3;

 

    cin.get();

    cin.get();

    cin.get();

    cin.get();

    cin.get();

}

G:科学计数法

#include<iostream>

#include <iomanip>//控制输出流

 

using namespace std;

void main()

{

    double db = 100 / 7.0;

    cout.setf(ios::fixed | ios::showpoint);//定点

    for (int i = 1; i < 10;i++)

    {

        cout.precision(i);//控制小数点多少位,输出1-10位的精度数值

        cout << db << endl;

    }

    cout << db << endl;

    db = 1000000000000000000000.0;

    //实数,根据方便自动选择质数或者定点小数输出

    cout.setf(ios::scientific, ios::fixed | ios::showpoint);

    cout << db << endl;

    cin.get();

}

运行结果:

H:setbase基数,清除历史遗迹

#include<iostream>

#include <iomanip>//控制输出流

 

using namespace std;

void main()

{

    const int num = 8848;

    cout << setw(10) << setfill('*') << setiosflags(ios::left) << num << endl;

    cout << setw(10) << setfill('*') << setiosflags(ios::right) << num << endl;

    cout << resetiosflags(ios::right) << setw(10) << setbase(8)

        << setfill('X') << setiosflags(ios::left) << num << endl;

    //resetioflags清楚历史遗迹

    //setw宽度

    //setbase基数,决定进制

 

    cin.get();

}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值