一、数据输出到文件(ofstream开启一个可供输出的文件)ios
C++文件操做包括输入、输出、拷贝、批处理。缓存
将简单的数据流输出到文件,只须要下面几步:session
首先要申明一个类的对象实例,将对象与文件相关联(也就是打开文件)。对这个流对象实例的输入输出操做就是对文件的操做。app
1 #include<fstream>//包含fstream文件 2 using namespace std; 3 4 int main() 5 { 6 ofstream outfile;//创建ofstream对象 7 outfile.open("test.txt");//将对象与文件关联 8 outfile<<"Hello,world!";//使用file对象将数据输出到test.txt文件中 9 outfile.close();//关闭与文件的链接 10 return 0; 11 }
程序运行结果:在程序源文件的同级目录下生成test.txt文件。函数
ps:若是指定的文件不存在,会有一个文件被产生出来并做为输出之用。若是指定的文件已经存在,这个文件会被开启做为输出之用,而文件中原已存在的数据会被丢弃。spa
因为对类ofstream,ifstream和fstream的对象所进行的第一个操做一般都是打开文件,这些类都有一个构造函数能够直接调用open函数,并拥有一样的参数。因此能够经过如下方法实现上面程序的第6行和第7行的效果:指针
ofstream outfile("test.txt");
二、成员函数open()code
函数原型:对象
void open( const char *_Filename, ios_base::openmode _Mode = ios_base::in | ios_base::out, int _Prot = (int)ios_base::_Openprot ); void open( const char *_Filename, ios_base::openmode _Mode ); void open( const wchar_t *_Filename, ios_base::openmode _Mode = ios_base::in | ios_base::out, int _Prot = (int)ios_base::_Openprot ); void open( const wchar_t *_Filename, ios_base::openmode _Mode );
参数说明:blog
_Filename The name of the file to open. 打开文件名 _Mode One of the enumerations in ios_base::openmode. 文件的打开方式(在ios_base::openmode中定义) _Prot The default file opening protection. 默认进行文件打开时的保护
ios_base::openmode中定义的打开方式:
ios::in, to permit extraction from a stream. 打开文件进行读操做,即读取文件中的数据 ios::out, to permit insertion to a stream. 打开文件进行写操做,即输出数据到文件中 ios::app, to seek to the end of a stream before each insertion. 打开文件以后文件指针指向文件末尾,只能在文件末尾进行数据的写入 ios::ate, to seek to the end of a stream when its controlling object is first created. 打开文件以后文件指针指向文件末尾,可是能够在文件的任何地方进行数据的写入 ios::trunc, to delete contents of an existing file when its controlling object is created. 默认的文件打开方式,若文件已经存在,则清空文件的内容 ios::binary, to read a file as a binary stream, rather than as a text stream. 打开文件为二进制文件,不然为文本文件
类ofstream,ifstream和fstream的成员函数open都包含了一个默认打开文件的mode:
1 ofstream -> ios::out|ios::trunc 2 ifstream -> ios::in 3 fstream -> ios::in|::ios::out
三、检验文件是否开启成功
文件有可能开启失败,在进行写入操做以前,咱们必须肯定文件的确开启成功。
(1)最简单的方法是检验class object的真伪:
1 #include<fstream>//包含fstream文件 2 #include<iostream>//cerr须要包含iostream头文件 3 using namespace std; 4 5 int main() 6 { 7 ofstream file("test.txt");//创建对象并将对象与文件关联 8 if(!file) 9 { 10 //若是file的计算结果为false,表示此文件并未开启成功 11 cerr<<"Oops!Unable to save session data!\n"; 12 } 13 file.close();//关闭与文件的链接 14 return 0; 15 }
ps:cerr表明标准错误输出设备。cerr和cout的区别:
①cerr不通过缓冲区,直接向显示器输出信息,而cout中的信息存放在缓冲区,缓冲区满或者遇到endl时才输出。。
②cout输出的信息能够重定向,而cerr只能输出到标准输出(显示器)上。
(2)调用成员函数is_open();
bool is_open();//返回bool值,为true表明文件成功打开
1 // ifstream::is_open 2 #include <iostream> 3 #include <fstream> 4 using namespace std; 5 6 int main () { 7 ifstream file("test.txt"); 8 9 if (file.is_open()) { 10 //其余代码 11 cout<<"Succeed opening file"; 12 } 13 else { 14 // show message: 15 cout<<"Error opening file"; 16 } 17 18 return 0; 19 }
四、关闭文件
当文件读写操做完成以后,咱们必须将文件关闭以使文件从新变为可访问的。
调用成员函数close()函数关闭文件,负责将缓存中的数据排放出来并关闭文件。
void close();
这个函数一旦被调用,初识声明的流对象就能够被用来打开其余文件,这个文件也能够重修被其余进程访问。
ps:为防止流对象被销毁时还关联着打开的文件,析构函数将会自动调用关闭函数close()。