From 2705599deb039fe348c86cd26c175371c795a3f9 Mon Sep 17 00:00:00 2001 From: wsb <1234@qq.com> Date: Sun, 9 Apr 2023 17:44:42 +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 --- fileReadWrite.cpp | 29 +++++++---------------------- fileReadWrite.h | 8 ++++---- main.cpp | 36 +++++++++++++++++++++--------------- test.txt | Bin 12 -> 400 bytes 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/fileReadWrite.cpp b/fileReadWrite.cpp index d3ee66e..f04f400 100644 --- a/fileReadWrite.cpp +++ b/fileReadWrite.cpp @@ -6,29 +6,14 @@ #include "iostream" #include "string" -/* -//空构造方法 -fileReadWrite::fileReadWrite() { - if (filePath.empty()) cout << "路径为空,请设置文件路径" << endl; - //初始化一些文件对象,便于openFile使用 - this->readStream=new ifstream ; - this->writeStream=new ofstream ; - this->ReadWriteStream=new fstream ; - -} - -fileReadWrite::fileReadWrite(string path) : filePath(path) { - if (path.length() == 0) cout << "文件路径为空!"; -}*/ - -fileReadWrite::fileReadWrite(char *path, ofstream *w) : +fileReadWrite::fileReadWrite(string path, ofstream *w) : filePath(path), writeStream(w) { if (path == "") cout << "文件路径为空!"; } -fileReadWrite::fileReadWrite(char *path, ifstream *r) : +fileReadWrite::fileReadWrite(string path, ifstream *r) : filePath(path), readStream(r) { if (path == "") cout << "文件路径为空!"; } @@ -51,7 +36,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) { return; } - writeStream->open(filePath, ios::app); //所有文件读追加到末尾 + writeStream->open(filePath); //所有文件读追加到末尾 //从控制台接收数据,敲击回车键结束输入 cout << "请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!" << endl; cin.getline(c, 100); //接收输入 @@ -72,7 +57,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) { return; } - writeStream->open(filePath, ios::in); //读模式打开 + readStream->open(filePath); //读模式打开 //把输入的字符写入到文件中 readStream->read(c, 100); //最大读取100个字符,并存储在char数组中 cout << "读取的文件完毕!:" << endl; @@ -96,7 +81,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) { if (flag == 0) { //写入文件 - ReadWriteStream->open(filePath, ios::app); + ReadWriteStream ->open(filePath,ios::app); //从控制台接收数据,敲击回车键结束输入 cout << "请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!" << endl; cin.getline(c, 100); //接收输入 @@ -106,7 +91,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) { } else if (flag == 1) { //开始读取 - ReadWriteStream->open(filePath, ios::in); //读模式打开 + ReadWriteStream->open(filePath,ios::in); //读模式打开 //把输入的字符写入到文件中 ReadWriteStream->read(c, 100); //最大读取100个字符,并存储在char数组中 cout << "读取的文件完毕,内容为:" << c << endl; @@ -127,6 +112,6 @@ void fileReadWrite::close() { if (ReadWriteStream != nullptr) ReadWriteStream->close(); } -fileReadWrite::fileReadWrite(char *path, fstream *w) : filePath(path), ReadWriteStream(w) { +fileReadWrite::fileReadWrite(string path, fstream *w) : filePath(path), ReadWriteStream(w) { } diff --git a/fileReadWrite.h b/fileReadWrite.h index 1795a93..2c6ff93 100644 --- a/fileReadWrite.h +++ b/fileReadWrite.h @@ -13,7 +13,7 @@ using namespace std; //文件的读取与写入 class fileReadWrite { public: - char* filePath; //文件路径 + string filePath; //文件路径 enum model { read, write, readWrite @@ -28,9 +28,9 @@ public: /*fileReadWrite(); fileReadWrite(string path);*/ - fileReadWrite(char* path,ofstream *writeStream); - fileReadWrite(char* path,ifstream *readStream); - fileReadWrite(char* path,fstream *ReadWriteStream); + fileReadWrite(string path,ofstream *writeStream); + fileReadWrite(string path,ifstream *readStream); + fileReadWrite(string path,fstream *ReadWriteStream); void openFile(model model, bool display); //打开文件,参数的含义是选择文件的操作模式、文件内容输出目的地 void close(); //关闭文件 diff --git a/main.cpp b/main.cpp index 41f94c9..7631a46 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include +#include #include "fileReadWrite.h" using namespace std; @@ -7,22 +8,27 @@ using namespace std; int main() { //文件路径: 在本项目路径下新建一个文本文件叫test.txt - string filePath = "test.txt"; - ifstream infile; - infile.open(filePath, ios::in); - if (!infile.is_open()) - { - cout << "读取文件失败" << endl; - } - //第二种读取方法 - char buf[1024]; - while (infile.getline(buf,sizeof(buf))) - { - cout << buf << endl; - } - - cout<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; } diff --git a/test.txt b/test.txt index bc7774a7b18deb1d7bd0212d34246a9b1260ae17..14fa6f0c627adc603e38647d5582ac81f5235aa1 100644 GIT binary patch literal 400 zcmX@|c-P^@YtCIdb$rX`%dZ#^z;b@_w|WK!fp7Mg!hoU;ryI8a0rD9ndOxpXWMH`P z6(XJhrTru!>S5x70T8|eP)lBB27@$^#sHQ(j6rrJ{QA!il1JEad$JwS4h5+AhMy35 zKS_vsn7Cj7gzo^knhvsI>k&*lCj8j{sR_l7zOUv$I|88M557a>VRpd8k?jBgB$tZj literal 12 Tcmc~u&B@7ED9