c++之流类库与输入输出

c++ —流类库与输入输出

IO流概念及流类库结构

流是信息流动的一种抽象
流对象与文件操作:
1、程序建立一个流对象
2、指定这个流对象与某个文件对象建立连接
3、程序操作流对象
4、流对象通过文件系统对所连接的文件对象产生作用
提取与插入:
1、读操作在流数据抽象中被称为(从流中)提取
2、写操作被称为向流中插入
在这里插入图片描述

输出流概述

最重要的三个输出流:
1、ostream
2、ofstream
3、ostringstream
预先定义好的输出流对象:
1、cout:标准输出
2、cerr:标准错误输出,没有缓冲,发送给它的内容立即被输出
3、clog:类似于cerr,但是有缓冲,缓冲区满时被输出
将标准输出重新定向到指定位置:
在这里插入图片描述
如何构造输出流对象:
在这里插入图片描述
文件输出流成员函数的三种类型:
1、与操作符等价的成员函数
2、执行非格式化写操作的成员函数
3、其它修改流状态且不同于操作符或插入运算符的成员函数
常用的文件输出流成员函数:
在这里插入图片描述

向文本文件输出

插入运算符"<<":
为所有标准c++数据类型预先设计的,用于传送字节到一个输出流对象
操纵符:
在这里插入图片描述
例:使用width控制输出宽度

#include <iostream>
using namespace std;

int main()
{
	double values[]={1.23,25.34,256.4567,12345.23};
	for(int i=0;i<4;i++){
		cout.width(10);
		cout<<values[i]<<endl;
	}
	return 0;
}

在这里插入图片描述
例2:使用setw操纵符来指定宽度

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	double values[]={1.23,25.34,256.4567,12345.23};
	string names[]={"Jhon","Make","Jim","Jimmy"};
	for(int i=0;i<4;i++){
		cout<<setw(6)<<names[i]<<endl;
		cout<<setw(10)<<values[i]<<endl;
	}
	return 0;
}

在这里插入图片描述
例3:设置对齐方式

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	double values[]={1.23,25.34,256.4567,12345.23};
	string names[]={"Jhon","Make","Jim","Jimmy"};
	for(int i=0;i<4;i++){
		cout<<setiosflags(ios_base::left)<<setw(6)<<names[i]<<endl;
		//左对齐 
		cout<<resetiosflags(ios_base::left)<<setw(10)<<values[i]<<endl;
		//将刚才设定的左对齐方式取消 
	}
	return 0;
}

在这里插入图片描述
setiosflags操纵符:
在这里插入图片描述
setiosflags的参数(流的格式标识)
在这里插入图片描述
精度:
1、浮点数输出精度默认值为6,如:3466.98
2、要改变精度,要用setprecision操纵符(定义在iomanip中)
3、如果不指定fixed或scientific,精度值则标示有效数字位数
4、如果设置了ios_base::fixed或ios_base::scientific,精度值则标示小数点之后的位数。
例1:未指定fixed或scientific

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	double values[]={1.23,25.37,256.4567,12345.23};
	string names[]={"Jhon","Make","Jim","Jimmy"};
	for(int i=0;i<4;i++){
		cout<<setiosflags(ios_base::left)<<setw(6)<<names[i]<<resetiosflags(ios_base::left)<<setw(10)<<setprecision(1)<<values[i]<<endl;;
	//未指定用哪种方式时,系统根据数字特点自动选择一种方式输出,但数据的有效位数都是一位 
	}
	return 0;
}

在这里插入图片描述
例2:指定fixed

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	double values[]={1.23,25.37,256.4567,12345.03};
	string names[]={"Jhon","Make","Jim","Jimmy"};
	cout<<setiosflags(ios_base::fixed); //以小数点固定的方式输出 
	for(int i=0;i<4;i++){
		cout<<setiosflags(ios_base::left)<<setw(6)<<names[i]<<resetiosflags(ios_base::left)<<setw(10)<<setprecision(1)<<values[i]<<endl;;
	//其含义变为:小数点后有一位
	}
	return 0;
}

在这里插入图片描述
例3:指定scientific

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	double values[]={1.23,25.37,256.4567,12345.03};
	string names[]={"Jhon","Make","Jim","Jimmy"};
	cout<<setiosflags(ios_base::scientific); //以科学计数法的方式输出 
	for(int i=0;i<4;i++){
		cout<<setiosflags(ios_base::left)<<setw(6)<<names[i]<<resetiosflags(ios_base::left)<<setw(10)<<setprecision(1)<<values[i]<<endl;;
	//其含义变为:表示小数的位数都为一位 
	}
	return 0;
}

在这里插入图片描述

向二进制文件输出

二进制文件流:
1、可以使用ofstream构造函数中的模式参量指定二进制输出模式
2、也可以以通常方式构造一个流,然后使用setmode成员函数,在文件打开后改变模式
3、通过二进制文件输出流对象完成输出
例:

#include <fstream> 
using namespace std;

struct Date{
	int year,month,day;
};
int main()
{
	Date dt={2001,1,7};
	ofstream file("date.txt",ios_base::binary);
	//第一个为要打开文件的路径,第二个为以二进制方式打开文件
	file.write(reinterpret_cast<char *>(&dt),sizeof(dt));
	//第一个参数是指向字符类型的指针,第二个参数是写的字节数,reinterpret_cast<char *>为临时类型转换 
	file.close();
	return 0;
}

向字符串输出

字符串输出流(ostringstream):
1、用于构造字符串
2、功能:
支持ofstream类的除open、close外的所有操作
str函数可以返回当前已构造的字符串
3、典型应用:将数值转换为字符串
例:用ostringstream将数值转换为字符串

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

//函数模板toString可将各种支持“<<”插入符的类型的对象转换为字符串 
template<class T>
inline string toString(const T &v){
	ostringstream os;//创建字符串输出流
	os<<v;//将变量v的值写入字符串流
	return os.str();//返回输出流生成的字符串 
}
int main()
{
	string str1=toString(234);
	cout<<str1<<endl;
	string str2=toString(1.22);
	cout<<str2<<endl;
	//若传入的是一个自定义类的对象,则必须重载插入运算符“<<” 
	return 0;
}

在这里插入图片描述

输入流概述

重要的输入流类:
1、istream类最适合用于顺序文本模式输入。cin是其实例
2、ifstream类支持磁盘文件输入
3、istringstream
构造输入流对象:
在这里插入图片描述
使用提取运算符“>>”从文本文件中输入:
在这里插入图片描述
输入流相关函数:
在这里插入图片描述
在这里插入图片描述

输入流应用举例

例1:get函数应用举例

#include <iostream>
using namespace std;

int main()
{
	char ch;
	while((ch=cin.get())!=EOF)//EOF为文件结束符,windows系统是Ctrl+z 
		cout.put(ch);
	return 0;
}

在这里插入图片描述
例2:为输入流指定一个终止字符

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string line;
	cout<<"Type a line terminated by 't'"<<endl;
	getline(cin,line,'t');//默认以换行为分隔符 
	cout<<line<<endl;
	return 0;
}

在这里插入图片描述
例3:从文件读入一个二进制记录到一个结构体中

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

struct SalaryInfo{
	unsigned id;
	double salary;
};
int main()
{
	SalaryInfo employee1={100001,8000};
	ofstream os("payroll",ios_base::out|ios_base::binary);
	os.write(reinterpret_cast<char *>(&employee1),sizeof(employee1));
	os.close();
	ifstream is("payroll",ios_base::in|ios_base::binary);
	if(is){
		SalaryInfo employee2;
		is.read(reinterpret_cast<char *>(&employee2),sizeof(employee2));
		cout<<employee2.id<<" "<<employee2.salary<<endl;
	}else{
		cout<<"ERROR:can't open file 'payroll'."<<endl;
	}
	is.close();
	return 0;
}

在这里插入图片描述
例4:用seekg函数设置位置指针

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	int values[]={3,7,0,5,4};
	ofstream os("integers",ios_base::out|ios_base::binary);
	os.write(reinterpret_cast<char *>(values),sizeof(values));
	os.close();
	
	ifstream is("integers",ios_base::in|ios_base::binary);
	if(is){
		is.seekg(3*sizeof(int));//移动读指针的字节数 
		int v;
		is.read(reinterpret_cast<char *>(&v),sizeof(int));
		cout<<"The 4th integer in the file 'integers' is "<<v<<endl;
	}else{
		cout<<"ERROR:can't open file 'integers'."<<endl;
	}
	return 0;
}

在这里插入图片描述
例5:读一个文件并显示其中0元素位置

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;


int main()
{
	ifstream file("integers",ios_base::in|ios_base::binary);
	if(file){
		while(file){//读到文件尾时file为0
			streampos here=file.tellg();//取得当前读指针的位置
			int v;
			file.read(reinterpret_cast<char *>(&v),sizeof(int));
			//每次读int个数,并将其放入v中
			if(file&&v==0)
				cout<<"Position "<<here<<" is 0."<<endl;
		}	
	} else{
		cout<<"ERROR:can't open file 'integers'."<<endl;
	}
	file.close();
	return 0;
}

在这里插入图片描述

从字符串输入

字符串输入流istringstream:
1、用于从字符串读取数据
2、在构造函数中设置要读取的字符串
3、功能:
支持ifstream类的除open、close外的所有操作
4、典型应用:将字符串转换为数值
例:将字符串转换为数值

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

template <class T>
inline T fromString(const string &str){
	istringstream is(str);//创建字符串输入流 
	T v;
	is>>v;//从字符串输入流中读取变量v 
	return v;//返回变量v 
}

int main()
{
	int v1=fromString<int>("5321");
	cout<<v1<<endl;
	double v2=fromString<double>("1.23");
	cout<<v2<<endl;
	return 0;
}

在这里插入图片描述

输入输出流

两个重要的输入输出流:
1、一个iostream对象可以是数据的源或目的。
2、两个重要的I/O流类都是从iostream派生的,它们是fstream和stringstream、这些类继承了前面叙述的istream和ostream类的功能。
fstream类:
在这里插入图片描述
stringstream类:
在这里插入图片描述

发布了30 篇原创文章 · 获赞 33 · 访问量 1269

猜你喜欢

转载自blog.csdn.net/weixin_45949075/article/details/104441259
今日推荐