亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 文件和流

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

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

Sr.No 数据类型和描述
1

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

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

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

您可以通过 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++ 编程时,您可以使用流插入运算符 (<<) 从程序中将信息写入文件,就像使用该运算符将信息输出到屏幕一样。唯一的区别是您使用 ofstreamfstream 对象而不是 cout 对象。

从文件中读取

您可以使用流提取运算符 (>>) 将文件中的信息读入程序,就像使用该运算符从键盘输入信息一样。唯一的区别是您使用 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() 函数忽略之前读取语句留下的额外字符。

文件位置指针

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

seekg 和 seekp 的参数通常是一个长整数。可以指定第二个参数来指示搜索方向。查找方向可以是 ios::beg (默认)相对于流的开头进行定位,ios::cur 用于相对于流或 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语言

  1. C++ 变量、文字和常量
  2. C++ 类和对象
  3. C++ 内存管理:新建和删除
  4. C++ 公共、受保护和私有继承
  5. C++朋友函数和朋友类
  6. C 文件处理
  7. Python 目录和文件管理
  8. C++ 中的结构和类
  9. C - 头文件
  10. C++ 日期和时间
  11. C++ 重载(运算符和函数)
  12. 什么是铣刀?