目录

C++ Files and Streams

到目前为止,我们一直在使用iostream标准库,它提供了cincout方法,分别用于读取标准输入和写入标准输出。

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

Sr.No 数据类型和描述
1

ofstream

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

2

ifstream

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

3

fstream

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

要在C ++中执行文件处理,头文件必须包含在C ++源文件中。

打开文件

必须先打开文件,然后才能从中读取或写入文件。 可以使用ofstreamfstream对象来打开文件进行写入。 并且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

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

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

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

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

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

关闭一个文件 (Closing a File)

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

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

void close();

写入文件

在进行C ++编程时,您可以使用流插入运算符(< 唯一的区别是你使用ofstreamfstream对象而不是cout对象。

从文件中读取 (Reading from a File)

您可以使用流提取运算符(>>)将文件中的信息读入程序,就像使用该运算符从键盘输入信息一样。 唯一的区别是你使用ifstreamfstream对象而不是cin对象。

读写示例

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

#include <fstream>
#include <iostream>
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.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

上面的例子使用来自cin对象的附加函数,比如getline()函数从外部读取行,而ignore()函数则忽略前一个read语句留下的额外字符。

文件位置指针

istreamostream提供了用于重新定位文件位置指针的成员函数。 这些成员函数是针对ostream的istream和seekp (“seek put”)的seekg (“seek get”)。

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

文件位置指针是一个整数值,它将文件中的位置指定为文件起始位置的字节数。 定位“get”文件位置指针的一些示例是 -

// position to the nth byte of fileObject (assumes ios::beg)
fileObject.seekg( n );
// position n bytes forward in fileObject
fileObject.seekg( n, ios::cur );
// position n bytes back from end of fileObject
fileObject.seekg( n, ios::end );
// position at end of fileObject
fileObject.seekg( 0, ios::end );
<上一篇.C++ Interfaces
↑回到顶部↑
WIKI教程 @2018