提交 2705599d 编写于 作者: W wsb

封装文件操作类

上级 0524c1e5
......@@ -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) {
}
......@@ -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(); //关闭文件
......
#include <iostream>
#include <vector>
#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<<buf<<endl;
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;
}
无法预览此类型文件
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册