流的概念和流类库的结构
程序的输入指的是从输入文件将数据传送给程序,程序的输出指的是从程序将数据传送给输出文件
c++输入输出包含以下三个方面的内容
- 对系统指定的标准设备的输入输出。即从键盘输入数据,输出到显示器屏幕,这种输入输出称为标准的输入输出,简称标准
I/O
- 以外存磁盘文件为对象进行输入和输出,即从磁盘文件输入数据,数据输出到磁盘文件。以外存文件为对象的输入输出称为文件的输入输出,简称文件
I/O
- 对内存中指定的空间进行输入和输出。通常指定一个字符数组作为存储空间(实际上可以利用该空间存储任何信息)。这种输入输出称为字符串输入输出,简称串
I/O
c++编译器系统提供了用于输入输出的iostream
类库,iostream这个单词是由3个部分组成,即i-o-stream,意为输入输出流。在iostream
类库中包含许多用于输入输出的类。
标准的输入流
-
cin.get缓冲区中读取一个字符
-
cin.get(两个参数)不读换行符
-
cin.getline()读取换行,并且扔掉
-
cin.ignore 忽略(N)N代表忽略字符数
-
cin.peek偷窥 偷看1个字符然后放回去
-
cin.putback 放回 把字符放回缓冲区
#include<iostream> using namespace std; /* cin.get()//一次只能读取一个字符 cin.get(一个参数)//读一个字符 cin.get(两个参数)//可以读字符串 cin.getline() cin.ignore() cin.peek() cin.putback() */ void test01() { //输入as 缓冲区中有 a s 和换行 第一个拿 a 第二个拿 s 第三个拿换行,第四次等待下次输入 char c=cin.get(); cout << "c=" << c<<endl; c = cin.get(); cout << "c=" << c << endl; c = cin.get(); cout << "c=" << c << endl; c = cin.get(); cout << "c=" << c << endl; } void test02() { //cin.get();//可以读字符串 char buf[1024]; cin.get(buf, 1024); char c = cin.get(); if (c == '\n') { cout << "换行还在缓冲区" << endl; } else { cout << "换行不在缓冲区" << endl; } cout << buf << endl; } //cin.get(两个参数)读取字符串时,不会把换行符拿走,遗留在缓冲区中 void test03() { char buf[1024]; cin.getline(buf, 1024); char c = cin.get(); if (c == '\n') { cout << "换行还在缓冲区" << endl; } else { cout << "换行不在缓冲区" << endl; } //cin.getline把换行符读取,并且扔掉 } void test04() { cin.ignore(2);//没有参数代表忽略一个字符,带参数n,代表忽略n个字符 char c=cin.get(); cout << "c=" << c << endl; } //cin.peek()偷窥 void test05() { //输入as 偷看一眼a然后再放回缓冲区,缓冲区中还是as char c = cin.peek(); cout << "c=" << c << endl; c = cin.get(); cout << "c=" << c << endl; } //cin.putback()数据 void test06() { char c = cin.get(); cin.putback(c); char buf[1024]; cin.getline(buf, 1024); cout << buf << endl; } int main() { //test01(); //test02(); //test03(); //test04(); //test05(); test06(); system("pause"); return 0; }
输入流案例
-
判断用户输入的是字符串还是数字,利用偷窥或者放回
-
让用户输入指定范围内的数字,如果不正确请重新输入
//案列1 判断用户输入的是字符串,还是数组? void test07() { cout << "请输入一串数字或者字符串" << endl; //偷窥 char c = cin.peek(); if (c >= '0'&&c <= '9') { int num; cin >> num; cout << "您输入的是数字,数字为" << num << endl; } else { char buf[1024]; cin >> buf; cout << "您输入的是字符串,字符串为" << buf << endl; } } //案例2 让用户输入1到10的数字,如果输入有误,重新输入 void test08() { int num; cout << "请输入一个1到10的数字:" << endl; while (true) { cin >> num; if (num > 0 && num <= 10) { cout << "输入的数字为" << num << endl; break; } //cout << "对不起请重新输入" << endl; //重置标志位 cin.clear();//重置标志位 cin.sync();//清空缓冲区 //cout << "标志位" << cin.fail() << endl; //标志位0正常的 1不正常 } } int main() { //test01(); //test02(); //test03(); //test04(); //test05(); //test06(); //test07(); test08(); system("pause"); return 0; }
标准输出流
#include<iostream>
using namespace std;
/*
cout.put() //向缓冲区写字符
cout.write()//从buffer中写num个字节到当前输出流中
*/
void test01()
{
//cout.put('a').put('b');
char buf[1024] = "helloworld";
cout.write(buf, strlen(buf));
}
int main()
{
test01();
system("pause");
return 0;
}
标准输出流
#include<iostream>
#include<iomanip>//使用控制符头文件
using namespace std;
/*
cout.put() //向缓冲区写字符
cout.write()//从buffer中写num个字节到当前输出流中
*/
void test01()
{
//cout.put('a').put('b');
char buf[1024] = "helloworld";
cout.write(buf, strlen(buf));
}
void test02()
{
//通过流成员函数
int number = 99;
cout.width(20);
cout.fill('*');
cout.setf(ios::left);//输入内容做对齐
cout.unsetf(ios::dec);//卸载十进制
cout.setf(ios::hex);//变为16进制
cout.setf(ios::showbase);//强制输出整数的基数 0 0x
cout.unsetf(ios::hex);//卸载十六进制
cout.setf(ios::oct);//变为8进制
cout << number << endl;
}
//控制符的方式显示
void test03()
{
int number = 99;
cout << setw(20)
<< setfill('^')
<< setiosflags(ios::showbase)//基数
<< setiosflags(ios::left)//左对齐
<< hex //十六进制
<< number
<< endl;
}
int main()
{
test03();
system("pause");
return 0;
}