1、标准的输入流
- cin.get 缓冲区中读取一个字符
- cin.get(两个参数) 不读换行符
- cin.getline () 读取换行 并且扔掉
- cin.ignore 忽略 (N) N代表忽略字符数
- cin.peek 偷窥 偷看1个字符然后放回去
- cin.putback 放回 把字符放回缓冲区
#define _CRT_SECURE_NO_WARNINGS
#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(两个参数) 读取字符串时,不会把换行符拿走,遗留在缓冲区中
//cin.getline()
void test03()
{
char buf[1024];
cin.getline(buf, 1024);
char c = cin.get();
if (c == '\n')
{
cout << "换行还在缓冲区" << endl;
}
else
{
cout << "换行不在缓冲区" << endl;
}
//cin.getline 把换行符读取 并且扔掉
}
//cin.ignore()
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;
}
//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;
}
2、输入流案例
- 判断用户输入的是字符串还是数字 利用偷窥 或者 放回
- 让用户输入指定范围内的数字,如果不正确 重新输入
- cin.fail() 看标志位 0正常 1不正常
- cin.clear()重置标志位
- cin.syne() 清空缓冲区
#define _CRT_SECURE_NO_WARNINGS
#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(两个参数) 读取字符串时,不会把换行符拿走,遗留在缓冲区中
//cin.getline()
void test03()
{
char buf[1024];
cin.getline(buf, 1024);
char c = cin.get();
if (c == '\n')
{
cout << "换行还在缓冲区" << endl;
}
else
{
cout << "换行不在缓冲区" << endl;
}
//cin.getline 把换行符读取 并且扔掉
}
//cin.ignore()
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;
}
//cin.putback() 放回
void test06()
{
char c = cin.get();
cin.putback(c);
char buf[1024];
cin.getline(buf, 1024);
cout << buf << endl;
}
//案例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;
}
}
//案例1 让用户输入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;
}
3、标准输出流
cout.flush() //刷新缓冲区 Linux下有效
cout.put() //向缓冲区写字符
cout.write() //从buffer中写num个字节到当前输出流中。
流对象的成员函数
- 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);
- cout << number << endl;
控制符
int number = 99;
cout << setw(20)
<< setfill('~')
<< setiosflags(ios::showbase) //基数
<< setiosflags(ios::left) //左对齐
<< hex // 十六进制
<< number
<< endl;
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <iomanip>
using namespace std;
//cout.flush() //刷新缓冲区 Linux下有效
//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()
{
//test01();
//test02();
test03();
system("pause");
return 0;
}
4、文件操作
写文件
- ofstream ofs
- open 指定打开方式
- isopen 判断是否打开成功
- ofs << “数据”
- ofs.close
读操作
- ifstream ifs
- 指定打开方式 ios::in
- isopen判断是否打开成功
- 三种方式读取数据
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//文件读写头文件
#include<fstream>
//写文件
void test01()
{
//以输出方式打开文件
//ofstream ofs("./test.txt", ios::out | ios::trunc);
ofstream ofs;
//后期指定打开方式
ofs.open("./test.txt", ios::out | ios::trunc);
//判断是否打开成功
if (!ofs.is_open())
{
cout << "打开失败" << endl;
}
//写数据
ofs << "姓名:abc" << endl;
ofs << "性别:男" << endl;
ofs << "年龄:100" << endl;
ofs.close();
}
//读文件
void test02()
{
ifstream ifs;
ifs.open("./test.txt", ios::in);
//判断是否打开成功
if (!ifs.is_open())
{
cout << "打开失败" << endl;
}
//第一种方式
//char buf[1024];
//while (ifs >> buf) //按行读取
//{
// cout << buf << endl;
//}
//第二种方式
//char buf2[1024];
//while (!ifs.eof()) //eof是读到文件尾部
//{
// ifs.getline(buf2, sizeof(buf2));
// cout << buf2 << endl;
//}
//第三种 不推荐 按单个字符读取
char c;
while ((c = ifs.get() )!= EOF) //EOF文件尾
{
cout << c;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}