c++文件流基本用法(fstream, ifstream, ostream) (转)

前言:
c++的文件流处理其实很简单,前提是你能够理解它。文件流本质是利用了一个buffer中间层。有点类似标准输出和标准输入一样。
c++ IO的设计保证IO效率,同时又兼顾封装性和易用性。本文将会讲述c++文件流的用法。
有错误和疏漏的地方,欢迎批评指证。
需要包含的头文件:
名字空间: std
也可以试用<fstream.h>

fstream提供了三个类,用来实现c++对文件的操作。(文件的创建,读写)。
ifstream – 从已有的文件读
ofstream – 向文件写内容
fstream - 打开文件供读写
支持的文件类型
实际上,文件类型可以分为两种: 文本文件和二进制文件.
文本文件保存的是可读的字符, 而二进制文件保存的只是二进制数据。利用二进制模式,你可以操作图像等文件。用文本模式,你只能读写文本文件。否则会报错。

例一: 写文件
声明一个ostream变量
[list=1]
[]调用open方法,使其与一个文件关联
[
]写文件
[]调用close方法.
[/list][list=1]
[
]#include <fstream.h>

[*]

[*]void main

[*]{

[*]ofstream file;

[*]

[*]file.open(“file.txt”);

[*]

[*]file<<“Hello file/n”<<75;

[*]

[*]file.close();

[*]}

[/list]

可以像试用cout一样试用操作符<<向文件写内容.
Usages:
[list=1]
[*]

[*]file<<“string/n”;

[*]file.put(‘c’);

[/list]

例二: 读文件

  1. 声明一个ifstream变量.
  2. 打开文件.
  3. 从文件读数据
  4. 关闭文件.
    [list=1]
    [*]#include <fstream.h>

[*]

[*]void main

[*]{

[*]ifstream file;

[*]char output[100];

[*]int x;

[*]

[*]file.open(“file.txt”);

[*]

[*]file>>output;

[*]cout<<output;

[*]file>>x;

[*]cout<<x;

[*]

[*]file.close();

[*]}

同样的,你也可以像cin一样使用>>来操作文件。或者是调用成员函数
Usages:

[/list]

[list=1]
[*]

[*]file>>char *;

[*]file>>char;

[*]file.get(char);

[*]file.get(char *,int);

[*]file.getline(char *,int sz);

[*]file.getline(char *,int sz,char eol);

[/list]

1.同样的,你也可以使用构造函数开打开一个文件、你只要把文件名作为构造函数的
第一个参数就可以了。
[list=1]
[*]ofstream file(“fl.txt”);

[*]ifstream file(“fl.txt”);

[/list]

上面所讲的ofstream和ifstream只能进行读或是写,而fstream则同时提供读写的功能。
void main()
[list=1]
[*]{

[*]fstream file;

[*]

[*]file.open(“file.ext”,iso::in|ios::out)

[*]

[*]//do an input or output here

[*]

[*]file.close();

[*]}

[/list]

open函数的参数定义了文件的打开模式。总共有如下模式
[list=1]
[*]属性列表

[*]

[*]ios::in 读

[*]ios::out 写

[*]ios::app 从文件末尾开始写

[*]ios::binary 二进制模式

[*]ios::nocreate 打开一个文件时,如果文件不存在,不创建文件。

[*]ios::noreplace 打开一个文件时,如果文件不存在,创建该文件

[*]ios::trunc 打开一个文件,然后清空内容

[*]ios::ate 打开一个文件时,将位置移动到文件尾

[/list]

Notes
[list]
[]默认模式是文本
[
]默认如果文件不存在,那么创建一个新的
[]多种模式可以混合,用|(按位或)
[
]文件的byte索引从0开始。(就像数组一样)
[/list]我们也可以调用read函数和write函数来读写文件。

文件指针位置在c++中的用法:
[list=1]
[*]ios::beg 文件头

[*]ios::end 文件尾

[*]ios::cur 当前位置
例子:

[/list]

[list=1]
[*]file.seekg(0,ios::end);

[*]

[*]int fl_sz = file.tellg();

[*]

[*]file.seekg(0,ios::beg);

[/list]
常用的错误判断方法:

[list=1]
[*]good() 如果文件打开成功

[*]bad() 打开文件时发生错误

[*]eof() 到达文件尾
例子:

[/list]

[list=1]
[*]char ch;

[*]ifstream file(“kool.cpp”,ios::in|ios::out);

[*]

[*]if(file.good()) cout<<"The file has been opened without problems;

[*]else cout<<"An Error has happend on opening the file;

[*]

[*]while(!file.eof())

[*]{

[*]file>>ch;

[*]cout<<ch;

[*]}

[/list]