From 1b4e42c3db4fee6a3d1856152123f04c8e5a40c6 Mon Sep 17 00:00:00 2001 From: wsb <1234@qq.com> Date: Sun, 9 Apr 2023 14:05:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E6=96=87=E4=BB=B6=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- fileReadWrite.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++++++ fileReadWrite.h | 41 +++++++++++++++ 3 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 fileReadWrite.cpp create mode 100644 fileReadWrite.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d36ff2..5864281 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(cpp_14_file) set(CMAKE_CXX_STANDARD 11) -add_executable(cpp_14_file main.cpp) +add_executable(cpp_14_file main.cpp fileReadWrite.cpp fileReadWrite.h) diff --git a/fileReadWrite.cpp b/fileReadWrite.cpp new file mode 100644 index 0000000..1e9903a --- /dev/null +++ b/fileReadWrite.cpp @@ -0,0 +1,127 @@ +// +// Created by 11010 on 2023/4/9. +// + +#include "fileReadWrite.h" +#include "io.h" + +//空构造方法 +fileReadWrite::fileReadWrite() { + this->filePath = ""; + cout << "路径为空,请设置文件路径" << endl; +} + +fileReadWrite::fileReadWrite(string path) : filePath(path) { + if (path.length() == 0) cout << "文件路径为空!"; +} + +fileReadWrite::fileReadWrite(string path, ofstream *writeStream) : + filePath(path), writeStream(writeStream) { + if (path.length() == 0) cout << "文件路径为空!"; + + +} + +fileReadWrite::fileReadWrite(string path, ofstream *writeStream, ifstream *readStream) : + filePath(path), writeStream(writeStream), readStream(readStream) { + if (path.length() == 0) cout << "文件路径为空!"; +} + +void fileReadWrite::openFile(fileReadWrite::model model, bool display) { + char c[100]; //保存控制台输入,最大字数不超过100个 + + //路径为空,给出提示结束函数运行 + if (filePath.empty()) { + cout << "文件路径为空!" << endl; + return; + } + + if (writeStream == nullptr){ + cout << "写入对象为空,没有被初始化!" << endl; + return; + } + + if (readStream == nullptr){ + cout << "读取对象为空,没有被初始化!" << endl; + return; + } + + if (ReadWriteStream == nullptr){ + cout << "读写对象为空,没有被初始化!" << endl; + return; + } + + switch (model) { + //如果是写文件 + case write: + writeStream->open(filePath, ios::app); //所有文件读追加到末尾 + //从控制台接收数据,敲击回车键结束输入 + cout<<"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"<write(c,100); + + //判断是显示到屏幕还是写入到文件 + if (display) { + cout<<"你输入的数据为:"<open(filePath, ios::in); //读模式打开 + //把输入的字符写入到文件中 + readStream->read(c,100); //最大读取100个字符,并存储在char数组中 + cout<<"读取的文件完毕!:"<>flag; + + if (flag==0){ + //写入文件 + ReadWriteStream->open(filePath,ios::app); + //从控制台接收数据,敲击回车键结束输入 + cout<<"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"<write(c,100); + cout<<"写入文件完成,你写入的内容是:"<open(filePath, ios::in); //读模式打开 + //把输入的字符写入到文件中 + ReadWriteStream->read(c,100); //最大读取100个字符,并存储在char数组中 + cout<<"读取的文件完毕,内容为:"<close(); + if (readStream != nullptr) readStream->close(); +} + +fileReadWrite::fileReadWrite(string path, ofstream *writeStream, ifstream *readStream, fstream *ReadWriteStream) : + filePath(path), writeStream(writeStream), readStream(readStream),ReadWriteStream(ReadWriteStream) +{ + +} diff --git a/fileReadWrite.h b/fileReadWrite.h new file mode 100644 index 0000000..4032b1f --- /dev/null +++ b/fileReadWrite.h @@ -0,0 +1,41 @@ +// +// Created by 11010 on 2023/4/9. +// + +#ifndef CPP_14_FILE_FILEREADWRITE_H +#define CPP_14_FILE_FILEREADWRITE_H + +#include "iostream" +#include "string" +#include //操作文件的头文件 + +using namespace std; + +//文件的读取与写入 +class fileReadWrite { +public: + string filePath; //文件路径 + + enum model { + read, write, readWrite + } model1; //读写模式枚举 + + bool display = false; //是否把数据显示到屏幕 + + ofstream *writeStream= nullptr; //写文件对象 + ifstream *readStream= nullptr; //读文件对象 + fstream *ReadWriteStream= nullptr; //读写文件 + + fileReadWrite(); + + fileReadWrite(string path); + fileReadWrite(string path,ofstream *writeStream); + fileReadWrite(string path,ofstream *writeStream,ifstream *readStream); + fileReadWrite(string path,ofstream *writeStream,ifstream *readStream,fstream *ReadWriteStream); + + void openFile(model model, bool display); //打开文件,参数的含义是选择文件的操作模式、文件内容输出目的地 + void close(); //关闭文件 +}; + + +#endif //CPP_14_FILE_FILEREADWRITE_H -- GitLab