C++文件操作

文章介绍了C++中使用fstream库进行文件读写的基本概念,包括ofstream用于写入文件,ifstream用于读取文件,以及fstream同时具备读写功能。文章还详细列出了不同的文件打开模式,如追加、读取、写入和截断,并提供了一个包含文件读写类的代码实例,展示如何创建、打开和关闭文件流。
摘要由CSDN通过智能技术生成

目录

一 文件读写类

二 文件的打开模式

三 代码实例


一 文件读写类

从文件读取流和向文件写入流所使用的标准库 是fstream,它定义了三个新的数据类型:

数据类型

描述

ofstream

该数据类型表示输出文件流,用于创建文件并向文件写入信息。

ifstream

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

fstream

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

二 文件的打开模式

模式标志

描述

ios::app

追加模式。所有写入都追加到文件末尾。

ios::ate

文件打开后定位到文件末尾。

ios::in

打开文件用于读取。

ios::out

打开文件用于写入。

ios::trunc

如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。

三 代码实例

//
// Created by 11010 on 2023/4/9.
//

#ifndef CPP_14_FILE_FILEREADWRITE_H
#define CPP_14_FILE_FILEREADWRITE_H


#include <fstream>  //操作文件的头文件

using namespace std;

//文件的读取与写入
class fileReadWrite {
public:
    string filePath;  //文件路径

    enum model {
        read, write, readWrite
    } model1;  //读写模式枚举

    bool display = false; //是否把数据显示到屏幕

    ofstream *writeStream;  //写文件对象
    ifstream *readStream;   //读文件对象
    fstream *ReadWriteStream; //读写文件

    /*fileReadWrite();
    fileReadWrite(string path);*/

    fileReadWrite(string path,ofstream *writeStream);
    fileReadWrite(string path,ifstream *readStream);
    fileReadWrite(string path,fstream *ReadWriteStream);

    void openFile(model model, bool display);  //打开文件,参数的含义是选择文件的操作模式、文件内容输出目的地
    void close(); //关闭文件
};


#endif //CPP_14_FILE_FILEREADWRITE_H





//
// Created by 11010 on 2023/4/9.
//

#include "fileReadWrite.h"
#include "iostream"
#include "string"

fileReadWrite::fileReadWrite(string path, ofstream *w) :
        filePath(path), writeStream(w) {
    if (path == "") cout << "文件路径为空!";


}

fileReadWrite::fileReadWrite(string path, ifstream *r) :
        filePath(path), readStream(r) {
    if (path == "") cout << "文件路径为空!";
}

void fileReadWrite::openFile(fileReadWrite::model model, bool display) {
    char c[100];  //保存控制台输入,最大字数不超过100个

    //路径为空,给出提示结束函数运行
    if (filePath == "") {
        cout << "文件路径为空!" << endl;
        return;
    }


    switch (model) {
        //如果是写文件
        case write:
            if (writeStream == nullptr) {
                cout << "写入对象为空,没有被初始化!" << endl;
                return;
            }

            writeStream->open(filePath); //所有文件读追加到末尾
            //从控制台接收数据,敲击回车键结束输入
            cout << "请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!" << endl;
            cin.getline(c, 100); //接收输入
            //把输入的字符写入到文件中
            writeStream->write(c, 100);

            //判断是显示到屏幕还是写入到文件
            if (display) {
                cout << "你输入的数据为:" << endl;
                cout << c << endl;
            }
            break;

            //如果是读文件
        case read:
            if (readStream == nullptr) {
                cout << "读取对象为空,没有被初始化!" << endl;
                return;
            }

            readStream->open(filePath); //读模式打开
            //把输入的字符写入到文件中
            readStream->read(c, 100); //最大读取100个字符,并存储在char数组中
            cout << "读取的文件完毕!:" << endl;
            //判断是显示到屏幕还是写入到文件
            if (display) {
                cout << "读取的文件内容如下:" << endl;
                cout << c << endl;
            }
            break;

            //如果是读写文件
        case readWrite:
            if (ReadWriteStream == nullptr) {
                cout << "读写对象为空,没有被初始化!" << endl;
                return;
            }

            int flag;
            cout << "请输入文件的操作模式,输入0代表写,输入1代表读" << endl;
            cin >> flag;

            if (flag == 0) {
                //写入文件
                ReadWriteStream ->open(filePath,ios::app);
                //从控制台接收数据,敲击回车键结束输入
                cout << "请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!" << endl;
                cin.getline(c, 100); //接收输入
                //把输入的字符写入到文件中
                ReadWriteStream->write(c, 100);
                cout << "写入文件完成,你写入的内容是:" << c << endl;

            } else if (flag == 1) {
                //开始读取
                ReadWriteStream->open(filePath,ios::in); //读模式打开
                //把输入的字符写入到文件中
                ReadWriteStream->read(c, 100); //最大读取100个字符,并存储在char数组中
                cout << "读取的文件完毕,内容为:" << c << endl;
            } else {
                cout << "你输入有误,程序结束!" << endl;
            }

            break;
    }
}


//关闭文件对象的方法
void fileReadWrite::close() {
    //只要读写对象不为空,则调用close方法释放对象
    if (writeStream != nullptr) writeStream->close();
    if (readStream != nullptr) readStream->close();
    if (ReadWriteStream != nullptr) ReadWriteStream->close();
}

fileReadWrite::fileReadWrite(string path, fstream *w) : filePath(path), ReadWriteStream(w) {

}







#include <iostream>
#include <vector>
#include "fileReadWrite.h"

using namespace std;


int main() {

    //文件路径: 在本项目路径下新建一个文本文件叫test.txt
    string filePath = "..\\test.txt"; //注意文件路径的写法,因为当前的程序编译路径不一致,所有需要用两个点回退到父级目录才能找到

    //初始化读写文件对象
    ifstream *in=new ifstream ;
    ofstream *on=new ofstream ;
    fstream  *io=new fstream ;

    //初始化读文件封装类对象
    /*fileReadWrite *frw=new fileReadWrite(filePath,in);
    frw->openFile(fileReadWrite::read, true);
    frw->close();*/

    //初始化写文件封装类对象
   /* fileReadWrite *fwr2=new fileReadWrite(filePath,on);
    fwr2->openFile(fileReadWrite::write, true);
    fwr2->close();*/

   //初始化读写文件封装类对象
    fileReadWrite *fwr3=new fileReadWrite(filePath,io);
    fwr3->openFile(fileReadWrite::readWrite, true);
    fwr3->close();

    return 0;
}

代码clone地址: git@gitcode.net:XiaoWang_csdn/cpp_14_file.git

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值