您的位置:首页 > 编程语言 > C语言/C++

从零开始学C++之IO流类库(二):文件流(fstream, ifstream, ofstream)的打开关闭、流状态

2015-03-05 09:34 363 查看
转:http://blog.csdn.net/jnu_simba/article/details/9383433

一、文件流

ofstream,由ostream派生而来,用于写文件

ifstream,由istream派生而来, 用于读文件

fstream,由iostream派生而来,用于读写文件

二、打开文件

说明了流对象之后,可使用函数open()打开文件。文件的打开即是在流与文件之间建立一个连接

函数原型

void open(const char * filename, int mode = ios::out,int prot = _SH_DENYNO);

参数

filename:文件的名称,可以包含(绝对和相对)路径

mode:文件打开模式

prot:保护模式

(一)、文件打开模式

打开方式 描述

ios::in 打开一个供读取的文件(ifstream流的默认值)

ios::out 打开一个供写入的文件(ofstream流的默认值)

ios::app 在写之前找到文件尾

ios::ate 打开文件后立即将文件定位在文件尾

ios::trunc 废弃当前文件内容

ios::nocreate(已不再支持) 如果要打开的文件并不存在,那么以此参数调用open()函数将无法进行

ios::noreplace (已不再支持) 如果要打开的文件已存在,试图用open()函数打开时将返回一个错误。

ios::binary 以二进制的形式打开一个文件,默认为文本文件

(二)、保护模式

#define _SH_DENYRW 0x10 /* deny read/write mode */拒绝对文件进行读写

#define _SH_DENYWR 0x20 /* deny write mode */拒绝写入文件

#define _SH_DENYRD 0x30 /* deny read mode */拒绝文件的读取权限

#define _SH_DENYNO 0x40 /* deny none mode */读取和写入许可

#define _SH_SECURE 0x80 /* secure mode */共享读取,独占写入

注意:假设A进程以_SH_DENYRW 打开,那么是B进程不能再对文件进行读写。

(三)、文件打开模式的有效组合



上述所有的打开模式组合还可以添加ate模式。对这些模式添加ate模只会改变文件打开时的初始定位,在第一次读或

写之前,将文件定位于文件末尾处。

(四)、文件打开的几点说明

1、文件打开也可以通过构造函数打开,例如:

ofstream fout(“out.txt“,ios::out);

2、文件的打开方式可以为上述的一个枚举常量,也可以为多个枚举常量构成的按位或表达式。

3、使用open成员函数打开一个文件时,若由字符指针参数所指定的文件不存在,则建立该文件。(out)

4、当打开方式中不含有ios::ate或ios::app选项时,则文件指针被自动移到文件的开始位置,即字节地址为0的位置。

5、从效果上看ofstream指定out模式等同于指定了out和trunc模式

6、默认情况下,fstream对象以in和out模式同时打开。

7、当文件同时以in和out打开时不会清空

8、如果只使用out模式,而不指定in模式,则文件会清空现有数据。

9、如果同时指定了out与app,不会清空

10、如果打开文件时指定了trunc模式,则无论是否同时指定了in模式,文件同样会被清空

三、流状态



对应于这个标志字各状态位,ios类还提供了以下成员函数来检测或设置流的状态:

bool rdstate(); //返回流的当前状态标志字

bool eof(); //返回非0值表示到达文件尾

bool fail(); //返回非0值表示操作失败

bool bad(); //返回非0值表示出现错误

bool good(); //返回非0值表示流操作正常

bool clear(int flag=0); //将流的状态设置为flag

为提高程序的可靠性,应在程序中检测I/O流的操作是否正常。当检测到流操作出现错误时,可以通过异常处理来解决问题。

四、文件的关闭

每个文件流类中都提供有一个关闭文件的成员函数close()

功能:当打开的文件操作结束后,就需要关闭它,使文件流与对应的物理文件断开联系,并能够保证最后输出到文件缓冲区中的内容,无论是否已满,都将立即写入到对应的物理文件中

函数原型:void close();

文件流对应的文件被关闭后,还可以利用该文件流调用open成员函数打开其他的文件,最好先clear 一下。

C++ Code
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

#include <cassert>

#include <iostream>

#include <fstream>

using namespace std;

int main(void)

{

/******************************************************/

//若不存在文件,会创建文件

//ofstream fout;

//fout.open("test.txt");

ofstream fout("test.txt", ios::out | ios::app);

//判断流状态

//if (fout.is_open())

//{

// cout<<"succ"<<endl;

//}

//else

// cout<<"failed"<<endl;

//if (fout.good())

//{

// cout<<"succ"<<endl;

//}

//else

// cout<<"failed"<<endl;

//if (fout)

//{

// cout<<"succ"<<endl;

//}

//else

// cout<<"failed"<<endl;

//if (!fout)

//{

// cout<<"failed"<<endl;

//}

//else

// cout<<"succ"<<endl;

assert(fout);

fout.close();

/************************************************************/

// 若文件不存在,不会创建文件

ifstream fin("test2.txt");

// assert(fin); //文件不存在,断言失败

fin.close();

/**********************************************************/

//ofstream fout1("test3.txt", ios::in | ios::out | ios::ate);

//ofstream fout2("test3.txt", ios::in | ios::out | ios::ate);

//ofstream fout1("test3.txt", ios::in | ios::out | ios::app);

//ofstream fout2("test3.txt", ios::in | ios::out | ios::app);

// app 和 ate 可以共存,以app为准

ofstream fout1("test3.txt", ios::in | ios::out | ios::app | ios::ate);

ofstream fout2("test3.txt", ios::in | ios::out | ios::app | ios::ate);

assert(fout1);

assert(fout2);

fout1 << "X";

fout2 << "Y"; //Y 输出在 X 的后面

fout1.close();

fout2.close();

/****************************************************************/

// app 与 trunc 不能共存,流状态为fail

ofstream fout3("test.txt", ios::out | ios::app | ios::trunc);

if (fout3.good())

{

cout << "good" << endl;

}

if (fout3.bad())

{

cout << "bad" << endl;

}

if (fout3.fail())

{

cout << "fail" << endl;

}

if (fout3.eof())

{

cout << "eof" << endl;

}

fout3.clear();

fout3.open("test.txt"); // clear之后能够重新open

if (fout3)

{

cout << "open succ" << endl;

}

else

cout << "open failed" << endl;

fout3.close();

return 0;

}
参考:

C++ primer 第四版

Effective C++ 3rd

C++编程规范
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐