C++输出流

版权声明:版权所有 如有侵权 概不追究 https://blog.csdn.net/weixin_40903417/article/details/88081580

最重要的三个输出流

ostream

ofstream

ostringstream

预先定义的输出流对象

cout 标准输出

cerr 标准错误输出,没有缓冲,发送给它的内容立即被输出

clog 类似于cerr,但是有缓冲,缓冲区满时被输出

构造输出流对象

ofstream类支持磁盘文件输出

如果在构造函数中指定一个文件名,当构造这个文件时该文件是自动打开的

ofstream myFile("filename");

可以在调用默认构造函数之后使用open成员函数打开文件

ofstream myFile;//声明一个静态文件输出流对象

myFile.open("filename");//打开文件,使流对象与文件建立联系

在构造对象或用open打开文件时可以指定模式

ofstream myFile("filename",ios_base::out|ios_base::binary);//打开一个用于输出的二进制文件

文件输出流成员函数的三种类型

与操纵符等价的成员函数

执行非格式化写操作的成员函数

其他修改流状态且不同于操纵符或插入运算符的成员函数

文件输出流成员函数

open函数

把流与一个特定的磁盘文件关联起来,需要指定打开模式

put函数

把一个字符写到输出流中

write函数

将内存中的一块内容写到一个文件输出流中

seekptellp函数

操作文件流的内部指针

close函数

关闭与一个文件输出流关联的磁盘文件

错误处理函数

在写到一个流时进行错误处理

插入<<运算符

为所有标准C++数据类型预先设计的,用于传送字节到一个输出流对象

使用width控制输出宽度

#include "stdafx.h"
#include <iostream>

using namespace std;


int main()
{
	double a[]={1.22,3.235,7.4322,9.3};
	for (int i = 0; i < 4; i++)
	{
		cout.width(10);
		cout<<a[i]<<endl;
	}
	system("Pause");
	return 0;
}

使用setw操纵符指定宽度

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	double a[]={1.22,3.235,7.4322,9.3};
	string b[]={"Tom","Jude","Marry","Jerry"};
	for (int i = 0; i < 4; i++)
	{
		cout<<setw(8)<<a[i]<<setw(6)<<b[i]<<endl;
	}
	system("Pause");
	return 0;
}

设置对齐方式

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	double a[]={1.22,3.235,7.4322,9.3};
	string b[]={"Tom","Jude","Marry","Jerry"};
	for (int i = 0; i < 4; i++)
	{
		cout<<setiosflags(ios_base::left)<<setw(8)<<a[i]<<resetiosflags(ios_base::left)<<setw(6)<<b[i]<<endl;//resetiosflags(ios_base::left)是将左对齐取消,恢复为默认的右对齐
	}
	system("Pause");
	return 0;
}

控制输出精度——未指定fixed或scientific

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	double a[]={1.22,32.35,743.22,9.3};
	string b[]={"Tom","Jude","Marry","Jerry"};
	for (int i = 0; i < 4; i++)
	{
		cout<<setiosflags(ios_base::left)
			<<setw(8)<<setprecision(1)<<a[i]<<resetiosflags(ios_base::left)
			<<setw(6)<<b[i]<<endl;//resetiosflags(ios_base::left)是将左对齐取消,恢复为默认的右对齐
	}
	system("Pause");
	return 0;
}

当未指定fixed或scientific时,setprecision括号中的参数表示有效数字的位数

控制输出精度——指定fixed或scientific

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	double a[]={1.22,32.35,743.22,9.3};
	string b[]={"Tom","Jude","Marry","Jerry"};
	cout<<setiosflags(ios_base::fixed);
	for (int i = 0; i < 4; i++)
	{
		cout<<setiosflags(ios_base::left)
			<<setw(8)<<setprecision(1)<<a[i]<<resetiosflags(ios_base::left)
			<<setw(6)<<b[i]<<endl;//resetiosflags(ios_base::left)是将左对齐取消,恢复为默认的右对齐
	}
	system("Pause");
	return 0;
}

指定fixed或scientific后,setprecision括号中的参数表示小数点后的位数

二进制文件流

使用ofstream构造函数中的模式参量指定二进制输出模式

以通常方式构造一个流,然后使用setmode成员函数,在文件打开后改变模式

通过二进制文件输出流对象完成输出

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

struct Date
{
	int year;
	int month;
	int day;
};
int main()
{
	Date a={2019,3,2};
	ofstream file("date.dat",ios_base::binary);
	file.write(reinterpret_cast<char*>(&a),sizeof(a));
	file.close();
	system("Pause");
	return 0;
}

wirte后第一个参数为指向数据块首地址的字符型指针,第二个参数为数据块所占内存的大小

将对象在内存中存储的二进制形式直接写到磁盘文件中

向字符串中输出

字符串输出流(ostringstream)

用于构造字符串

支持ofstream类的除open、close外的所有操作

str函数可以返回当前已构造的字符串

典型应用:将数值转换为字符串

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

template<class T>
string toString(const T &v)
{
	ostringstream s;
	s<<v;
	return s.str(); 
}
int main()
{
	string a=toString(2);
	string b=toString(1.2);
	cout<<a<<" "<<b<<endl;
	system("Pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40903417/article/details/88081580