文件重定向,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&gt


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

}

运行结果:

 

目录
相关文章
|
1月前
|
C#
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
34 1
|
JavaScript
JS: int转16进制
JS: int转16进制
65 0
|
Python
内置函数--bin() oct() int() hex()
bin(x) Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
140 0
|
Java Android开发 C++
【Android NDK 开发】JNI 方法解析 ( int 数组传递 | jintArray 类型 | 数组转换 | 获取数组长度 | 获取数组元素 | 指针遍历数组 | 数组返回值设置 )(二)
【Android NDK 开发】JNI 方法解析 ( int 数组传递 | jintArray 类型 | 数组转换 | 获取数组长度 | 获取数组元素 | 指针遍历数组 | 数组返回值设置 )(二)
636 0
|
Java Android开发 对象存储
【Android NDK 开发】JNI 方法解析 ( int 数组传递 | jintArray 类型 | 数组转换 | 获取数组长度 | 获取数组元素 | 指针遍历数组 | 数组返回值设置 )(一)
【Android NDK 开发】JNI 方法解析 ( int 数组传递 | jintArray 类型 | 数组转换 | 获取数组长度 | 获取数组元素 | 指针遍历数组 | 数组返回值设置 )(一)
771 0
|
数据安全/隐私保护 编解码 开发工具
char,Character,int,字符及编码日记
char,Character,int,字符及编码日记 public class Test { public static void main(String[] args) { char c = 'a'; Character ch = new Characte...
1181 0

热门文章

最新文章