diff --git a/fileReadWrite.cpp b/fileReadWrite.cpp index d3ee66ebae9cc1e15212518fe34ab4398f80436c..f04f400bb579295193324095f10b1c54d8ac9bf3 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 1795a93111214832beef9d2463fafcb4d63ac5cf..2c6ff93719d74ce7090f6a5fcc93f090f87ac4a7 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 41f94c94f9536b488e10c681c9a86dd678aaf472..7631a46e9528f7a2d5eb2528c04f3c1c162f6033 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 Binary files a/test.txt and b/test.txt differ