[C++]输入和输出流

在这里插入图片描述

cout,cin的本质是全局的流对象

cerr :标准错误,没有缓冲区

clog :标准日志,有缓冲区

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CN8PvTQ9-1582199663052)(http://47.97.124.78:8000/wp-content/uploads/2020/02/2.png)]

程序输入过程中,程序先读取输入缓冲区中内容如果没有,等待键盘将数据传入输入缓冲区,再经此到达程序。而程序输出过程中,程序先将数据写入输出缓冲区并不显示,直到读取endl(endl会先写入一个换行符,然后刷新缓冲区)才将数据显示在显示屏上

标准输入流

#include<iostream>
using namespace std;

//cin.get()函数练习
void Cin_Get_Text() {
	//无参数或者一个参数时表示读取一个字符
	char ch;
	while ((ch = cin.get()) != EOF) {
		//EOF是End Of File的缩写,在操作系统中表示资料源无更多的资料可读取
		//此时想要退出程序:CTRL+Z
		cout << ch << endl;
	}
	/*
	不难发现,如果一次输入多个字符,就会逐个换行打出
	这是由于一次输入的多个字符通过回车键一起刷新到缓冲区
	而程序在读取缓冲区的过程中是逐个读取的,所以逐个逐行输出
	*/


	//两个参数是,可以读取一个字符串
	char buf[256] = { 0 };
	cin.get(buf, 256);   //从缓冲区读取一个字符串
	cout << buf << endl;


	cin.getline(buf, 256);   //读取一行数据,不读取换行符
	cout << buf << endl;
}


//cin.ignore()函数练习
//忽略当前字符
//忽略的本质时将数据从缓冲区内移出,所以缓冲区内已经不包含忽略的数据了
void Cin_Ignore_Text() {
	char ch;
	cin.get(ch);  //从缓冲区取出数据,没有则暂时阻塞
	cout << "the first is " << ch << endl;
	cin.ignore();
	cin.get(ch);
	cout << "the second is " << ch << endl;
	/*运行结果,忽略了一个字符
	abcdefg   
	the first is a
	the second is c
	*/
	
	//可以设置一个参数,规定忽略几个字符
	cin.ignore(5);
	//可以设置两个参数,确定终止位置,会在遇到“\n”时提前终止忽略过程
	cin.ignore(5, '\n');
}


//cin.peek()的用法
//get是把数据直接从缓冲区取走,peek只是单纯获取到数据的值,数据仍然存储于缓冲区中
void Cin_Peek_Text() {
	cout << "请输入数组或字符串" << endl;
	char ch;
	char buf[256] = { 0 };
	ch = cin.peek();  //检查一下缓冲区,并返回第一个字符,不把数据取走
	//判断输入的数据内容
	if (ch >= '0' && ch <= '9') {
		cin.getline(buf, 256);
		cout << "你输入的是数字:" << buf << endl;
	}
	else {
		cin.getline(buf, 256);
		cout << "你输入的是字符串:" << buf << endl;
	}
	//最终判断结果正确而且打印出了完整的输入内容
	//证明peek只窥视了值,所有数据仍然保存在缓冲区中
}

//cin.putback()函数练习
//负责把拿出缓冲区的数据重新放回缓冲区,常与cin.get()配合使用
void Cin_Putback_Text() {
	cout << "请输入数组或字符串" << endl;
	char ch;
	char buf[256] = { 0 };
	ch = cin.get();  //从缓冲区取走字符
	if (ch >= '0' && ch <= '9') {
		//把ch放回缓冲区
		cin.putback(ch);
		cin.getline(buf, 256);
		cout << "你输入的是数字:" << buf << endl;
	}
	else {
		cin.putback(ch);
		cin.getline(buf, 256);
		cout << "你输入的是字符串:" << buf << endl;
	}
}

int main() {
	Cin_Peek_Text();
	return 0;
}

标准输出流

#include<iostream>
#include<iomanip>  //想要使用控制符方式输出需要引入此头文件
using namespace std;

//cout.flush()函数
//用来手动刷新缓冲区
//endl本身自带刷新缓冲区和换行
//在没有endl的情况下需要手动刷新缓冲区,才能将数据显示在屏幕上
void Cout_Flush_Text(){
	cout << "Hello World";
	cout.flush();
	cout << endl;
	//cout.put()函数,每次只能输出一个字符,不过支持链式编程
	cout.put('h').put('e').put('l').put('l').put('o') << endl;
	cout.write("Hello this is write func", strlen("Hello this is write func"));
}

//格式化输出
void text() {
	
	//成员方法的方式设置输出格式
	int num = 10;
	cout << "十进制下num是:" << num << endl;
	cout.unsetf(ios::dec); //卸载当前默认的十进制输出方式
	cout.setf(ios::oct); //按照八进制输出
	cout.setf(ios::showbase); //将八进制或十进制等的前导0显示出来
	cout << "八进制下num是:" << num<< endl;
	cout.width(10); //设置宽度
	cout.fill('*');  //设置空余位置填充
	cout.setf(ios::left);  //设置左对齐
	cout << "Hello" << endl;
	
	
	
	//通过控制符方式
	int num2 = 10;
	cout << hex
		<< setiosflags(ios::showbase)
		<< setw(10)
		<< setiosflags(ios::left)
		<< setfill('$')
		<< num2 << endl;
}


int main() {

	text();
	return 0;
}

文件操作

#include<iostream>
#include<fstream>   //文件读写需要引入此头文件
using namespace std;

//文本文件读写
void FileText() {
	ifstream ism("source.txt", ios::in);  //in表示只读方式打开文件,第一个参数传入文件路径
	/*文件打开方式二:
	ifstream ism;
	ism.open("source.txt", ios::in);
	*/
	if (!ism) {  //判断文件是否成功打开
		cout << "打开文件失败" << endl;
		return;
	}
	//开始读文件并写入目标文件
	char ch;
	ofstream osm("target.txt", ios::out);  //out表示写入文件
	//app表示追加数据进文件
	while (ism.get(ch)) { //逐个输出字符
		cout << ch;
		osm.put(ch);
	}
	//关闭文件
	ism.close();
	osm.close();
}


int main() {
	FileText();
	return 0;
}

二进制文件操作

#include<iostream>
#include<fstream>
using namespace std;
//二进制文件操作

class Person {
public:
	int age;
	int id;
	Person(){}
	Person(int age, int id) : age(age), id(id) {}
	void Show() {
		cout << "the age is " << age << " the id is " << id << endl;
	}
};



void BinaryFileText() {
	Person p1(10, 20), p2(30, 40);  //对象p1,p2在程序中是以二进制形式存放的
	//把p1,p2写进文件中
	ofstream osm("target.txt", ios::out | ios::binary);  //以二进制文件写入
	osm.write((char*)&p1,sizeof(Person));  //二进制方式写文件,且参数类型为char*
	osm.write((char*)&p2, sizeof(Person));  
	osm.close();  //关闭文件
	ifstream ism("target.txt",ios::in | ios::binary);  //binary以二进制方式读取文件
	Person p3,p4;
	ism.read((char*)&p3,sizeof(Person));  //read专门用来读取二进制文件
	ism.read((char*)&p4, sizeof(Person));
	p3.Show();
	p4.Show();
}


int main() {
	BinaryFileText();
	return 0;
}
发布了18 篇原创文章 · 获赞 2 · 访问量 234

猜你喜欢

转载自blog.csdn.net/renboyu010214/article/details/104416846