文件重定向,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 != ' ')//复合表达式

    {

        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(' ');

    char  str[] = "123456789abcdefg";

 

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

    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 << " " << endl;

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

    cout << num << "  " << num << " ";

    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();

}

执行结果:

 

原文地址:https://www.cnblogs.com/zhchoutai/p/6840538.html