开发手册 欢迎您!
软件开发者资料库

C ++文件和流

C ++文件和流 - 简单易学的学习C ++:初学者教程,包含C ++语法面向对象语言,方法,覆盖,继承,多态,接口,STL,迭代器,算法,异常处理,重载,模板,命名空间的完整知识和<br/>信号处理。

到目前为止,我们一直在使用 iostream 标准库,它提供了 cin cout 方法,用于从标准输入读取和写入标准输出.

本教程将教您如何从文件中读取和写入.这需要另一个名为 fstream 的标准C ++库,它定义了三种新的数据类型 :

Sr.No数据类型&说明
1

ofstream

此数据类型表示输出文件流,用于创建文件和将信息写入文件.

2

ifstream

此数据类型表示输入文件流,用于从文件中读取信息.

3

fstream

此数据类型通常表示文件流,并具有ofstream和ifstream的功能,这意味着它可以创建文件,将信息写入文件,以及从文件中读取信息.

要在C ++中执行文件处理,头文件< iostream>和< fstream>必须包含在C ++源文件中.

打开文件

必须先打开文件,然后才能读取或写入文件.可以使用 ofstream fstream 对象来打开文件进行写入.并且ifstream对象仅用于打开文件以供阅读.

以下是open()函数的标准语法,它是fstream,ifstream和ofstream对象的成员.

void open(const char *filename, ios::openmode mode);

这里,第一个参数指定要打开的文件的名称和位置,以及 open()的第二个参数成员函数定义应该打开文件的模式.

Sr.No模式标记&说明
1

ios :: app

追加模式.该文件的所有输出都附加到结尾.

2

ios :: ate

打开文件输出并将读/写控制移到结尾文件.

3

ios :: in

打开文件进行阅读.

4

ios :: out

打开文件进行写作.

5

ios :: trunc

如果文件已存在,则在打开文件之前将截断其内容.

您可以通过 OR 将两个或多个这些值组合在一起.例如,如果您想以写入模式打开文件并希望在已经存在的情况下截断它,则以下将是语法 :

ofstream outfile;outfile.open("file.dat", ios::out | ios::trunc );

类似地,您可以打开文件进行读写目的,如下所示;

  fstream afile;  afile.open("file.dat",ios :: out | ios :: in);

关闭文件

当C ++程序终止时,它会自动刷新所有流,释放所有已分配的内存和关闭所有打开的文件.但是程序员在程序终止之前应该关闭所有打开的文件总是一个好习惯.

以下是close()函数的标准语法,它是fstream的成员,ifstream ,和ofstream对象.

  void close();

写入文件

在进行C ++编程时,使用流将信息写入程序中的文件就像使用该运算符将信息输出到屏幕一样,插入运算符(<<<唯一的区别是您使用 ofstream fstream 对象而不是 cout 对象.

从文件中读取

使用流提取操作符(>>)将文件中的信息读入程序,就像使用该操作符从键盘输入信息一样.唯一的区别是您使用 ifstream fstream 对象而不是 cin 对象.

读写示例

以下是C ++程序,它以读写模式打开文件.在将用户输入的信息写入名为afile.dat的文件后,程序从文件中读取信息并将其输出到屏幕上 :

#include #include using namespace std; int main () {   char data[100];   // open a file in write mode.   ofstream outfile;   outfile.open("afile.dat");   cout << "Writing to the file" << endl;   cout << "Enter your name: ";    cin.getline(data, 100);   // write inputted data into the file.   outfile << data << endl;   cout << "Enter your age: ";    cin >> data;   cin.ignore();      // again write inputted data into the file.   outfile << data << endl;   // close the opened file.   outfile.close();   // open a file in read mode.   ifstream infile;    infile.open("afile.dat");     cout << "Reading from the file" << endl;    infile >> data;    // write the data at the screen.   cout << data << endl;      // again read the data from the file and display it.   infile >> data;    cout << data << endl;    // close the opened file.   infile.close();   return 0;}

编译并执行上述代码时,会产生以下样本输入和输出 :

$./a.outWriting to the fileEnter your name: ZaraEnter your age: 9Reading from the fileZara9

以上示例使用来自cin对象的其他函数,如getline()函数来读取来自外部的行和ignore()函数忽略前一个读取语句留下的额外字符.

文件位置指针

两者 istream ostream 提供用于重新定位文件位置指针的成员函数.这些成员函数是istream的 seekg ("seek get")和ostream的 seekp ("seek put").

seekg和seekp的参数通常是一个长整数.可以指定第二个参数来指示搜索方向.搜索方向可以是 ios :: beg (默认值),用于相对于流的开头定位, ios :: cur ,用于相对于当前位置的定位stream或 ios :: end 用于相对于流的结尾进行定位.

文件位置指针是一个整数值,用于指定文件中的位置作为文件起始位置的字节数.定位"get"文件位置指针的一些示例是 :

// position to the nth byte of fileObject (assumes ios::beg)fileObject.seekg( n );// position n bytes forward in fileObjectfileObject.seekg( n, ios::cur );// position n bytes back from end of fileObjectfileObject.seekg( n, ios::end );// position at end of fileObjectfileObject.seekg( 0, ios::end );