指定文件打开的模式: ofstream myfile("filename",ios_base::out | ios_base::binary ); //表示打开一个用于输出的文件,而且是个二进制文件

ofstream是个输出流类,这个流对象默认打开就是为了输出,所以ios_base::out可以不写

利用文件输出流提供的函数,通过对象对文件操作,通过输出流对象向文件写信息

常用输出流成员函数:
open函数:把流与一个特定的磁盘文件关联起来

                         put函数:把一个字符写到输出流中
                       
                         write函数:将内存中一块内容以原样的二进制形式写到一个文件输出流中,实现低级的二进制写操作的函数
                        
                        seekp和tellp函数:操作文件流的内部指针,都是操作文件流内部指针的函数,seekp移动文件流内部的写指针(在文件流内部隐含的有一个指针,指向文件夹中下一应该写入的位置)
                          
                                                        tellp返回当前文件流内部指针的指向位置

                         close函数:关闭一个文件输出流关联的磁盘文件

                        错误处理函数:在写到一个流时,进行错误处理

向文本文件输出:标准输出设备显示器被看作文本文件

向文本文件输出:使用插入运算符最方便

格式控制方式:使用操纵符 manipulator

所有操作符都定义在ios_base类中

控制输出宽度:setw和width,这两个仅影响紧随其后的输出项,其他流格式操纵符保持有效直到发生改变

//使用 width 控制输出宽度,不足的用空格补齐
#include "stdafx.h"
#include <iostream> 
using namespace std;  
int _tmain(int argc, _TCHAR* argv[])
{ 
	double values[] = { 1.23, 35.36, 653.7, 4358.24 }; 
	for (int i = 0; i < 4; i++)
	{ 
		cout.width(10);  
		cout << values[i] << endl;
	} 
	return 0;
}
//使用setw控制输出宽度,不足的部分用空格补齐
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string name[] = { "apple", "banana", "pear" };
	double num[] = { 1.2, 3.5, 7.8 };
	for (int i = 0; i < 3; i++)
	{
		cout << setw(6) << name[i] << setw(13) << num[i] << endl;
	}
	return 0;
}
···

设置对齐方式:  setiosflags(ios_base::left)    //左对齐,一直起作用
                          resetiosflags(ios_base::left)    //将左对齐设置取消

include "stdafx.h"

include

include

include

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string name[] = { "apple", "banana", "pear" };
double num[] = { 1.2, 3.5, 7.8 };
for (int i = 0; i < 3; i++)
{
cout <<setiosflags(ios_base::left)<< setw(6) << name[i] << resetiosflags(ios_base::left)<<setw(13) << num[i] << endl;
}
return 0;
}

控制输出精度:
                     **   精度值表示有效数字的位数。**
                        默认情况:浮点数输出精度都是6
                        要改变精度:setprecision操纵符,定义在iomanip头文件中,**要想改变一个数值的输出精度,采用setprecision**
                        不指定fixed(定点形式)或scientific(科学计数法),系统会根据输出大小自动选择一种输出模式,这时精度值表示有效数字位数
                    **    设置了ios_base::fixed或ios_base::scientific精度值都表示小数点后的位数**

include "stdafx.h"

include

include

include

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string name[] = { "apple", "banana", "pear" };
double num[] = { 1.28953, 3.54125, 7.841252 };
cout << setiosflags(ios_base::fixed);
for (int i = 0; i < 3; i++)
{
cout <<setiosflags(ios_base::left)<< setw(6) << name[i] << resetiosflags(ios_base::left)<<setw(13)<<setprecision(1) << num[i] << endl;
cout << setiosflags(ios_base::oct) << num[i] << endl;
}
return 0;
}


include "stdafx.h"

include

include

include

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string name[] = { "apple", "banana", "pear" };
double num[] = { 1.28953, 3.54125, 7.841252 };
cout << setiosflags(ios_base::scientific);
for (int i = 0; i < 3; i++)
{
cout <<setiosflags(ios_base::left)<< setw(6) << name[i] << resetiosflags(ios_base::left)<<setw(13)<<setprecision(1) << num[i] << endl;
cout << setiosflags(ios_base::oct) << num[i] << endl;
}
return 0;
}

向二进制文件输出(是留给后续程序读取处理):信息按文本格式输出时,内存中的内部存储形式到输出形式之间有种格式转换,转换要消耗时间,空间

构造ofstream对象时,指定二进制模式:也可以在构造时构造普通流,再用setmode成员函数,在文件打开之后改变模式

write要求第一个参数是指向字符类型的指针,第二个参数是写的字节数。**得到指针,准备写入数据块的起始地址**

include "stdafx.h"

include

include

using namespace std;
struct datement
{
int year;
int month;
int date;
};
int _tmain(int argc, _TCHAR* argv[])
{
datement today = {2018,11,9};
ofstream file("C:\Users\YMD\Desktop\123.txt",ios_base::binary);
file.write(reinterpret_cast<char*>(&today), sizeof(today));
file.close();
return 0;
}

向字符串输出:把内存中的一个字符串当作输出流的目的地
               功能:将其他格式的数据转换成字符串
字符串输出流:用于构造字符串,常用的场景是将数值转换成字符串。
             功能:支持ofstream类的除open,close外所有操作
                        str函数可以返回当前已构造的字符串

include "stdafx.h"

include

include

include

using namespace std;

template
string toString(const T &v)
{
ostringstream os; //创建字符串输出流
os << v;
return os.str();//返回输出流生成的字符串
}

int _tmain(int argc, _TCHAR* argv[])
{
string s1 = toString(1.56);
cout << s1 << endl;
return 0;
}

posted on 2018-09-11 11:05  泰坦妮克号  阅读(216)  评论(0编辑  收藏  举报