C++ 文件操作(一)

1、数据输出到文件(ofstream开启一个可供输出的文件)

C++文件操作包括输入、输出、拷贝、批处理。

  • ofstream:写操作(输出)的文件类(由ostream引申而来)
  •  ifstream:读操作(输入)的文件类(由istream引申而来)
  • fstream:可同时读写操作的文件类(由iostream引申而来)

将简单的数据流输出到文件,只需要下面几步:

首先要申明一个类的对象实例,将对象与文件相关联(也就是打开文件)。对这个流对象实例的输入输出操作就是对文件的操作。

 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:如果指定的文件不存在,会有一个文件被产生出来并作为输出之用。如果指定的文件已经存在,这个文件会被开启作为输出之用,而文件中原已存在的数据会被丢弃。

由于对类ofstream,ifstream和fstream的对象所进行的第一个操作通常都是打开文件,这些类都有一个构造函数可以直接调用open函数,并拥有同样的参数。所以可以通过以下方法实现上面程序的第6行和第7行的效果:

ofstream outfile("test.txt");

2、成员函数open()

函数原型:

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
);

参数说明:

_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

3、检验文件是否开启成功

    文件有可能开启失败,在进行写入操作之前,我们必须确定文件的确开启成功。

(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 }

4、关闭文件

当文件读写操作完成之后,我们必须将文件关闭以使文件重新变为可访问的。

调用成员函数close()函数关闭文件,负责将缓存中的数据排放出来并关闭文件。

void close();

这个函数一旦被调用,初识声明的流对象就可以被用来打开其他文件,这个文件也可以重修被其他进程访问。

ps:为防止流对象被销毁时还关联着打开的文件,析构函数将会自动调用关闭函数close()。

posted @ 2016-02-18 23:27  dqq_nlp  阅读(308)  评论(0编辑  收藏  举报