>">
赞
踩
引文:
输出流:ostream (output stream)
输出流:istream (input stream)
输入输出流:iostream
写操作(输出)的文件类 :ofstream
读操作(输入)的文件类:ifstream
可同时读写操作的文件类:fstream (file stream)
理解例子:
ofstream examplefile("example.txt");//声明类example并与创建的文件example.txt联系
其中examplefile是ofstream的对象,对象(examplefile)第一个操作通常与一个真正的文件(example.txt)联系起来,改文件由一个流对象(该例子为examplefile)来表示(这些类的一个实例),对改对象进行的操作就是对该对象联系的文件操作。
examplefile.is_open()//检查一个文件(examplefile对象所联系的文件)是否被顺利打开
bool is_open();
examplefile << "This is a line.\n";
examplefile << "This is another line.\n";
examplefile.close();
写入操作和关闭文件。close(),它负责将缓存中的数据排放出来并关闭文件。使用之后原对象可以访问其他文件。
ps:为防止流对象被销毁时还联系着打开的文件,析构函数(destructor)将会自动调用关闭函数close
代码示例:
- //建立一个文件并写入内容
- #include <fstream>
- using namespace std;
-
- int main()
- {
- ofstream examplefile("example.txt");
- if (examplefile.is_open())
- {
- examplefile << "This is a line.\n";
- examplefile << "This is another line.\n";
- examplefile.close();
- }
- return 0;
- }
理解例子:
补充:exit(): 头文件:“stdlib.h ” 或写 <cstdlib>
功 能: 关闭所有文件,终止正在执行的进程。
exit(0)表示正常退出,exit(x)(x不为0)都表示异常退出,这个x是返回给操作系统(包括UNIX,Linux,和MS DOS)的,以供其他程序使用
examplefile.eof();
eof ,它是ifstream 从类 ios 中继承过来的,当到达文件末尾时返回true
examplefile.getline(buffer,100);
getline,读取一行字符,直到遇见换行符。getline具有返回值:成功:返回读取的字节数 失败:返回-1。
代码示例:
- //对文件的读取示例
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- int main ()
- {
- char buffer[256]; //定义一个数组,用来存放字符
- ifstream examplefile("example.txt"); //声明一个对象与要读的文件联系
- if (! examplefile.is_open()) //
- {
- cout << "Error opening file"; exit (1);
- }
- while (!examplefile.eof())
- {
- examplefile.getline(buffer,100);
- cout<<buffer<< endl;
- }
- return 0;
- }

状态标识符的验证
bad() | 如果在读写过程中出错,返回 true 。例如:当我们要对一个不是打开为写状态的文件进行写入时,或者我们要写入的设备没有剩余空间的时候。 |
fail() | 除了与bad() 同样的情况下会返回 true 以外,加上格式错误时也返回true ,例如当想要读入一个整数,而获得了一个字母的时候。 |
eof() | 如果读文件到达文件末尾,返回true。(上文所用) |
good() | 这是最通用的:如果调用以上任何一个函数返回true 的话,此函数返回 false 。 |
tellg() 和 tellp() | 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get 流指针的位置 (用tellg) 或 put 流指针的位置(用tellp). |
seekg() 和seekp() | 这对函数分别用来改变流指针get 和put的位置。两个函数都被重载为两种不同的原型: |
代码示例:
- #include <iostream>
- #include <fstream>
- using namespace std;
-
- int main ()
- {
- const char * filename = "example.txt";
- long l,m;
- ifstream file(filename, ios::in|ios::binary);
- l = file.tellg();
- file.seekg(0, ios::end);
- m = file.tellg();
- file.close();
- cout <<"size of "<< filename;
- cout <<" is "<< (m-l)<<" bytes.\n";
- return 0;
- }

首先要知道以下标识符:
ios::out 为输出(写)而打开文件
ios::ate 初始位置:文件尾
ios::app 所有输出附加在文件末尾
ios::trunc 如果文件已存在则先删除该文件
ios::binary 二进制方式
ios::in 为输入(读)而打开文件
ios::end 流尾
上边代码利用文件二进制首尾位置,做差得出文件大小(二进制长度)。
read和write函数
分别为istream和ostream的成员函数,前者被ifstream所继承,后者被ofstream所继承。
代码示例:
- #include <fstream>
- #include <iostream>
- using namespace std;
- int main ()
- {
- const char * filename = "example.txt";
- char * buffer;//buffer是一块内存地址,用来存储或读取数据
- long size;//size 是一个整数值,表示要从缓存(buffer)中读出或写入的字符数
- ifstream file(filename, ios::in|ios::binary|ios::ate);
- size = file.tellg();
- file.seekg(0, ios::beg);
- buffer = new char [size];
- file.read(buffer, size);
- file.close();
- cout <<"the complete file is in a buffer";
- delete[] buffer;
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。