fstream读写文件

1.fstream是什么?

fstream是C++标准库中面向对象库的一个,用于操作流式文件。
fstream本质上是一个class,提供file操作的各种方法。

2.关系图

在这里插入图片描述
basic_fstream是一个类模板,暂且不用深入理解它。我们关心的事,它前面继承的那一堆东西。

fstream是basic_fstream<char>的一个模板类,也就说明,fstream也继承了一堆东西。

我们再关心一下从 ios_base基类,重点继承了什么?文件流的打开模式。
在这里插入图片描述

3.实验

3.1 打开/创建文件

void open( const char *filename, ios_base::openmode mode =ios_base::in|ios_base::out );

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    fstream fs;
    cout << "hello" << endl;
    fs.open("d://zhou");    
    if (fs.is_open())
    {
        cout << "open success" << endl;
    }
    else
    {
        cout << "open failed" << endl;
    }
    system("pause");
    return 0;
}

输出结果
在这里插入图片描述
打开失败了。
说明不能自动创建不存在的文件。

修改fs.open("d://zhou");fs.open("d://zhou", ios_base::in); ,即只读的方式打开。
运行后,文件依旧不能被创建。

修改fs.open("d://zhou");fs.open("d://zhou", ios_base::out); ,即只写的方式打开。
运行后,文件在D盘被创建了。
在这里插入图片描述在这里插入图片描述
所以,想要打开一个不存的文件,并且创建它,必须包含 ios_base::out模式。

3.2 写文件 write()

basic_ostream& write( const char_type* s, std::streamsize count );
它是继承于ostream类

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char str[] = "hello world";
    fstream fs;
    cout << "hello" << endl;
    fs.open("d://zhou.txt", ios_base::out); 
    if (fs.is_open())
    {
        cout << "open success" << endl;
    }
    else
    {
        cout << "open failed" << endl;
    }
    
    fs.write(str, sizeof(str) - 1);  //写入内容
    //fs << str << endl;
    
    system("pause");

    return 0;
}

实验结果
在这里插入图片描述
写入有两种方式,一种是使用 fs.write,另一种是使用 << 流操作符号。流操作符号本质也是调用了write方法。

3.3 读文件 read()

basic_istream& read( char_type* s, std::streamsize count );
它是继承于istream类。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    char str[] = "hello world";
    char rstr[128] = {0};
    fstream fs;

    cout << "hello" << endl;

    fs.open("d://zhou.txt", ios_base::out | ios_base::in); 
    if (fs.is_open())
    {
        cout << "open success" << endl;
    }
    else
    {
        cout << "open failed" << endl;
    }

    fs.write(str, sizeof(str) - 1);  //写入
    fs.sync();                       //写入硬盘
    fs.seekg(ios_base::beg);         //文件指针的位置回到开头
    fs.read(rstr, sizeof(rstr));     //读数据

    cout << rstr << endl;

    fs.close();
    system("pause");

    return 0;
}

实验结果
在这里插入图片描述
写入"hello word" 再读出来。
咦?怎么读一个数据不是只调用read就行吗?怎么多了好几个?
因为…
fs.write()是将内容写入缓冲区(内存)。fs.sync() 是为了将缓冲区的内容刷新写入硬盘。而read方法是只能读取硬盘上的内容,读不了缓冲区。
fs.seekg()则是将文件的指针回到开头。当为了写入之后,文件指针指向了末尾了。调用read时候,也就会从末尾读,啥也读不出来。

3.4 读文件 getline()

读文件的操作,getlineread更加常用。
getline一读就一整行了。
getline的内容实现也是依靠read方法(c语言是这样,c++可能也是这样)。

    while (!fs.eof()) 
    {
        fs.getline(rstr, sizeof(rstr));     //读数据
        cout << rstr << endl;
    }

fs.eof()是为了判断是否到达末尾,若抵达末尾,返回true,否则false。

实验结果
在这里插入图片描述

4.最后

fstream的方法何其之多,掌握比较常用的即可。许多操作跟C语言类似。
学习C++最重要的技能之一是学会查找文档。
中文手册:https://www.apiref.com/cpp-zh/cpp.html
英文手册:http://www.cplusplus.com/reference/

在这里插入图片描述

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值