C++ 笔记(21)— 处理文件(文件打开、关闭、读取、写入)

C++ 提供了 std::fstream ,旨在以独立于平台的方式访问文件。 std::fstreamstd::ofstream 那里继承了写入文件的功能,并从 std::ifstream 那里继承了读取文件的功能。换句话说, std::fstream 提供了读写文件的功能。

数据类型描述
ofstream该数据类型表示输出文件流,用于创建文件并向文件写入信息。
ifstream该数据类型表示输入文件流,用于从文件读取信息。
fstream该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。

要使用 std::fstream 类或其基类,需要包含头文件 <fstream>

#include <fstream>

C++ 编程中,我们使用流插入运算符( << )向文件写入信息,就像使用该运算符输出信息到屏幕上一样。唯一 不同的是,在这里使用的是 ofstreamfstream 对象,而不是 cout 对象。

同理,我们使用流提取运算符( >> )从文件读取信息,就像使用该运算符从键盘输入信息一样。唯一不同的是,在这里您使用的是 ifstreamfstream 对象,而不是 cin 对象。

1. 使用open( )和close( )打开和关闭文件

要使用 fstreamofstreamifstream 类,需要使用方法 open() 打开文件, open() 函数是 fstreamifstreamofstream 对象的一个成员。

fstream myFile;
myFile.open("HelloFile.txt",ios_base::in|ios_base::out|ios_base::trunc);

if (myFile.is_open()) // check if open() succeeded
{
    // do reading or writing here
    myFile.close();
}

open() 接受两个参数:

  • 第一个是要打开的文件的路径和名称(如果没有提供路径,将假定为应用程序的当前目录设置);
  • 第二个是文件的打开模式。
模式标志描述
ios::app追加模式。所有写入都追加到文件末尾
ios::ate文件打开后定位到文件末尾
ios::in以只读方式打开文件
ios::out以只写方式打开文件
ios::trunc导致现有文件被覆盖,这是默认设置
ios_base::binary创建二进制文件(默认为文本文件)

可以把以上两种或两种以上的模式结合使用。例如,如果您想要以写入模式打开文件,并希望截断文件,以防文件已存在,那么您可以使用下面的语法:

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

类似地,您如果想要打开一个文件用于读写,可以使用下面的语法:

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

在上述代码中,指定了模式 ios_base::trunc (即便指定的文件存在,也重新创建它)、 ios_base::in (可读取文件)和 ios_base::out (可写入文件)。

注意到在上述代码中使用了 is_open() ,它检测 open() 是否成功。

注意:保存到文件时,必须使用 close() 关闭文件流。 close() 函数是 fstreamifstreamofstream 对象的一个成员。

还有另一种打开文件流的方式,那就是使用构造函数:

fstream myFile("HelloFile.txt",ios_base::in|ios_base::out|ios_base::trunc);

如果只想打开文件进行写入,可使用如下代码:

ofstream myFile("HelloFile.txt", ios_base::out);

如果只想打开文件进行读取,可使用如下代码:

ifstream myFile("HelloFile.txt", ios_base::in);

注意:无论是使用构造函数还是成员方法 open() 来打开文件流,都建议您在使用文件流对象前,使用 open() 检查文件打开操作是否成功。

2. 使用open( )创建文本文件并使用运算符<<写入文本

有打开的文件流后,便可使用运算符 << 向其中写入文本。

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ofstream myFile;
    myFile.open("HelloFile.txt", ios_base::out);

    if (myFile.is_open())
    {
        cout << "File open successful" << endl;

        myFile << "My first text file!" << endl;
        myFile << "Hello file!" << endl;

        cout << "Finished writing to file, will close now" << endl;
        myFile.close();
    }

    return 0;
}

第 8 行以 ios_base::out 模式(即只写模式)打开文件。第 10 行检查 open( ) 是否成功,然后使用插入运算符 << 写入该文件流。最后,第 18 行关闭文件流。

3. 使用open( )和运算符>>读取文本文件

要读取文件,可使用 fstreamifstream ,并使用标志 ios_base::in 打开它。

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    ifstream myFile;
    myFile.open("HelloFile.txt", ios_base::in);

    if (myFile.is_open())
    {
        cout << "File open successful. It contains: " << endl;
        string fileContents;

        while (myFile.good())
        {
            getline (myFile, fileContents);
            cout << fileContents << endl;
        }

        cout << "Finished reading file, will close now" << endl;
        myFile.close();
    }
    else
    cout << "open() failed: check if file is in right folder" << endl;

    return 0;
}

请注意,这里没有使用提取运算符 >> 将文件内容直接读取到第 18 行使用 cout 显示的 string ,而是使用 getline() 从文件流中读取输入。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值