标准输入输出

标准输入输出

1624853954352

1624853711081

缓冲区

1
2
3
4
5
6
7
有缓冲区的输入输出对象
cout //标准输出
cin //标准输入
clog //打印日志

没有缓冲区的输出对象
cerr //标准出错

1624853775895

标准输入流 常用的cin成员方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ch 代表字符变量
buff 代表字符数组
size代表字符数组长度

cin.get() //一次只能读取一个字符
cin.get(ch) //读一个字符
cin.get(buff,size) //可以读字符串
cin.getline(buff,size)//输入一行

cin.ignore() //从缓冲区取走一个字符并丢弃
cin.ignore(2) //从缓冲区取走2个字符并丢弃
cin.ignore(2,'\n') //从缓冲区取走2个字符并丢弃,遇到\n提前结束

cin.peek();//偷窥缓冲区内容,返回缓冲区第一个字符,不取走字符

cin.putback(ch) //把ch放回缓冲区
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;


//cin的operator>>操作符 //根据回车来刷新缓冲区 根据空格来隔离每个变量的内容
void test1()
{
int myInt;
long myLong;

char buf[128] = { 0 };

cin >> myInt;
cin >> myLong;
cin >> buf;

cout << "myInt: " << myInt << endl;
cout << "myLong: " << myLong << endl;
cout << "buf: " << buf << endl;
}


//cin.get()方法
void test2()
{
char ch;

//cin.get()如果读到的不是EOF标识符,那么会永远的阻塞等待
//从键盘来讲ctrl+z 代表EOF标识符
while ((ch = cin.get() )!= EOF) {
cout << ch << endl;
}
}

void test3()
{
char a, b, c;

char buf[10] = { 0 };

cout << "从输入缓冲区去读取数据,如果缓冲区中没有数据,就阻塞" << endl;
//cin.get(a); //从输入缓冲区去读取数据,如果有就给a
//cin.get(b);
//cin.get(c);
//cin.get(a).get(b).get(c);


//cout << "a =" << a << endl;
//cout << "b =" << b<< endl;
//cout << "c =" << c<< endl;

cin.get(buf, 10, ' ');

cout << buf << endl;
}

//cin.getline()
void test4()
{
char buf[128] = { 0 };
cout << "请输入一个字符串 aa bb cc dd" << endl;
cin.getline(buf, 128); //从输入缓冲区中读数据到buf中,最多读128 ,知道遇到\n为止

cout << "buf:" <<buf << endl;
}


//cin.ignore()
void test5()
{
char buf1[128];
char buf2[128];

cout << "请输入一个字符串 aa bb cc dd" << endl;
cin >> buf1; //aa
cin.ignore(2);
cin.getline(buf2, 128);// bb cc dd

cout << "buf1:" << buf1 << endl;
cout << "buf2:" << buf2 << endl;

}

//cin.putback()
void test6()
{
cout << "请输入一个数字或者字符串" << endl;
char ch;
ch = cin.get(); //从输入缓冲区去读一个字符
if ((ch >= '0') && ch <= '9') {
cout << "输入的是一个数字" << endl;
int num;
//此时数字第一个字符已经读出来了。 需要将ch放回到输入缓冲区
cin.putback(ch); //将ch仍会缓冲区, 位置就缓冲区的头部。
cin >> num;

cout << "num =" << num << endl;
}
else {
cout << "输入的是一个字符串" << endl;
char buf[128] = { 0 };
//cin.putback(ch);
cin.getline(buf, 128);

cout << "buf:" << buf << endl;
}
}

int main(void)
{
//test1();
//test2();
//test3();
//test4();
//test5();
test6();

return 0;
}

标准输出流 cout常用的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
cout.flush() //刷新缓冲区

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

cout.write(str,strLen) //输出一个字符串

cout.width(10) //设置10个位宽
cout.fill('*') //位宽空余的地方用*代替

cout.setf(ios::oct) //设置八进制输出
cout.unsetf(ios::dex) //卸载当前默认的十进制输出方式
cout.setf(ios::showbase);//把八进制的0和十六进制的0x显示出来
cout.setf(ios::left);//设置左对齐

cout常用控制符

1
2
3
4
5
6
7
8
9
flush
endl
oct
dec
hex
setbase
setw
setfill
setprecision

1624857186021

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <iomanip>

using namespace std;

/*
cout.put()
cout.write() //向输出缓冲区中写数据
cout.width()
cout.fill()
cout.setf(标记)


操作符、控制符
flush
endl
oct
dec
hex
setbase
setw
setfill
setprecision
*/

void test1()
{
cout << "hello " << endl;
cout.put('h').put('e').put('l') << endl;

char *str = "hello wolrd";
cout.write(str, strlen(str));
cout << endl;
cout.write(str, strlen(str) - 1);
cout << endl;
}


void test2()
{
/*
//使⽤类成员函数
cout << "<start>";
cout.width(30); //设置接下来要输出的长度,是30
cout.fill('*'); //将没有填充的多余的空间,填充成*
cout.setf(ios::showbase); //#include <iomanip>
cout.setf(ios::internal); //设置
cout << hex << 123 << "<End>\n";
cout << endl;
*/


//使⽤ 操作符、控制符
cout << "<Start>"
<< setw(30)
<< setfill('*')
<< setiosflags(ios::showbase) //基数
<< setiosflags(ios::internal)
<< hex
<< 123
<< "<End>\n"
<< endl;

}

void test3()
{
#if 0
int a;
cout << "input a:";
cin >> a;
cout << "dec:" << dec << a << endl; //以⼗进制形式输出整数
cout << "hex:" << hex << a << endl; //以⼗六进制形式输出整数a
cout << "oct:" << setbase(8) << a << endl; //以⼋进制形式输出整数a

const char *pt = "China"; //pt指向字符串"China"
cout << setw(10) << pt << endl; //指定域宽为,输出字符串
cout << setfill('*') << setw(10) << pt << endl; //指定域宽,输出字符串,空⽩处以'*'填>充
#endif

double pi = 22.0 / 7.0; //计算pi值
//按指数形式输出,8位⼩数
cout << setiosflags(ios::scientific) << setprecision(8);
cout << "pi=" << pi << endl; //输出pi值
cout << "pi=" << setiosflags(ios::fixed) << pi << endl; //改为⼩数形式输出

//cout << "pi=" << setprecision(4) << pi << endl; //改为位⼩数
}

void test4()
{
double a = 123.456, b = 3.14159, c = -3214.67;
cout << setiosflags(ios::fixed) << setiosflags(ios::right) << setprecision(2);
cout << setw(10) << a << endl;
cout << setw(10) << b << endl;
cout << setw(10) << c << endl;

// 123.45
// 3.14
// -3214.67
}
int main(void)
{

//test1();
//test2();
//test3();
test4();
return 0;
}