1、概述
用c++仿真经常需要从文本中读数据或写写数据,为了方便使用写了一个简单的文本数据读写模型。
2、模型接口
myfile.h
/*******************************************************************
* 文件名称: myfile.h
* 简要描述:
*
* 作者: Gyf
* 日期: 12/8/2019
* 说明:
******************************************************************/
#ifndef __MYFILE_H_
#define __MYFILE_H_
#include "iostream"
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
const char Endl = '\n';
#define TO_NUMBER(s, n) (istringstream(s) >> n) //字符串转换为数值
#define TO_STRING(n) (((ostringstream&)(ostringstream() << n)).str()) //数值转换为字符串
class MyStr // 为了方便处理字符串定义
{
public:
MyStr & operator << (string & s);
MyStr & operator >> (string & val);
MyStr & operator >> (int & val);
MyStr & operator >> (unsigned int & val);
MyStr & operator >> (double & val);
MyStr & operator >> (float & val);
MyStr & operator >> (bool & val);
private:
string sub_get();
string data;
};
class myFILE // 读写文件类
{
public:
myFILE(ios_base::openmode type, string path); // 创建文件流
~myFILE();
bool endfile; // 读入文件状态
/* 读文件 */
myFILE & operator >> (string& s);
MyStr & operator >> (int& val);
MyStr & operator >> (unsigned int& val);
MyStr & operator >> (double& val);
MyStr & operator >> (float& val);
MyStr & operator >> (bool& val);
/* 写文件 */
myFILE & operator << (const string& s);
myFILE & operator << (const int& s);
myFILE & operator << (const unsigned int& s);
myFILE & operator << (const float& s);
myFILE & operator << (const double& s);
myFILE & operator << (const bool& s);
myFILE & operator << (const char& s);
/* 读文件时光标置于文件首行 */
void init();
private:
ios_base::openmode type;
string path;
MyStr data;
ifstream* in;
ofstream* out;
};
#endif // __MYFILE_H_
/************* Gyf **** myfile.h **** 12/8/2019 ******************************************************************************************/
测试样例
main.cpp
int main(int argc, char** argv)
{
int val_int;
bool val_bool;
double val_double;
float val_float;
string val_string = "";
/* 写文件 */
{
myFILE outfile(ios_base::out | ios_base::trunc, "data.txt"); // 写文件
val_string = "val_int val_bool val_double val_float";
outfile << val_string << Endl;
for (int i = 0; i < 5; i++)
{
val_int = i;
val_bool = (bool)(i % 2);
val_double = i * 2.0;
val_float = i / 2.0;
outfile << val_int << val_bool << val_double << val_float << Endl;
}
}
val_string = "";
/* 读文件 */
{
myFILE infile(ios_base::in, "data.txt"); // 读文件
infile >> val_string;
std::cout << val_string <<endl;
for (int i = 0; i < 5; i++)
{
infile >> val_int >> val_bool >> val_double >> val_float;
std::cout << val_int << " " << val_bool << " " << val_double << " " << val_float << endl;
}
}
std::cout << __DATE__ << " " << __TIME__ << endl;
std::system("pause");
return 0;
}
运行结果
3、源码
/* myfile.cpp*/
#include "myfile.h"
MyStr & MyStr::operator >> (string & val)
{
val = this->data;
return *this;
}
MyStr & MyStr::operator << (string & s)
{
this->data = s;
return *this;
}
MyStr & MyStr::operator >> (int & val)
{
string sub = this->sub_get();
if (sub.length() > 0)
TO_NUMBER(sub, val);
return *this;
}
MyStr & MyStr::operator >> (unsigned int & val)
{
string sub = this->sub_get();
if (sub.length() > 0)
TO_NUMBER(sub, val);
return *this;
}
MyStr & MyStr::operator >> (double & val)
{
string sub = this->sub_get();
if (sub.length() > 0)
TO_NUMBER(sub, val);
return *this;
}
MyStr & MyStr::operator >> (float & val)
{
string sub = this->sub_get();
if (sub.length() > 0)
TO_NUMBER(sub, val);
return *this;
}
MyStr & MyStr::operator >> (bool & val)
{
string sub = this->sub_get();
if (sub.length() > 0)
TO_NUMBER(sub, val);
return *this;
}
string MyStr::sub_get()
{
string substr = "";
int index = -1;
int len = this->data.length();
if (len > 0)
{
char fc;
fc = this->data.at(0);
while (fc == ' ' || fc == '\n' || fc == '\t')
{
this->data.erase(0, 1);
len--;
if (len > 0)
fc = this->data.at(0);
else
break;
}
if (len > 0)
{
index = (int)(this->data.find(" "));
if (index == -1)
{
index = (int)(this->data.find("\n"));
if (index == -1)
index = (int)(this->data.find("\t"));
}
}
if (index == -1)
{
substr = this->data;
this->data = "";
}
else
{
substr = this->data.substr(0, index);
this->data.erase(0, index);
}
}
return substr;
}
/**
* 函数名称: myFILE
* 功能描述: 创建文件
* @myFILE_TYPE type:文件类型
* in input File 读的方式打开文件
* out output 写的方式打开文件
* binary binary 二进制方式打开
* ate at end 打开的时候定位到文件末尾
* app append 所有操作都定位到文件末尾
* trunc truncate 丢弃打开前文件存在的内容
* @string path:文件路径
* @return
*/
myFILE::myFILE(ios_base::openmode type, string path)
{
this->type = type;
this->path = path;
string s = "";
this->data << s;
this->in = NULL;
this->out = NULL;
if ((type & ios_base::in) == (ios_base::in))
{
this->in = new ifstream(path, type);
}
else if ((type & ios_base::out) == (ios_base::out))
{
this->out = new ofstream(path, type);
}
}
myFILE::~myFILE()
{
if (this->in != NULL)
{
if (this->in->is_open())
{
this->in->close();
}
this->in->clear();
delete this->in;
}
if (this->out != NULL)
{
if (this->out->is_open())
{
this->out->close();
}
this->out->clear();
delete this->out;
}
}
myFILE & myFILE::operator >> (string& s)
{
s = "";
if (this->in != NULL)
if(this->in->is_open())
getline(*this->in, s);
if (s == "" || s == "\n" || s == " ")
{
this->endfile = true;
}
else
{
this->endfile = false;
}
return *this;
}
MyStr & myFILE::operator >> (int& val)
{
string lines = "";
if (this->in != NULL)
{
if (!this->in->is_open())return this->data;
//*this->in >> lines;
getline(*this->in, lines);
if (lines == "" || lines == "\n" || lines == " ")
{
this->endfile = true;
}
else
{
this->endfile = false;
}
this->data << lines;
this->data >> val;
}
return this->data;
}
MyStr & myFILE::operator >> (unsigned int& val)
{
string lines = "";
if (this->in != NULL)
{
if (!this->in->is_open())return this->data;
getline(*this->in, lines);
if (lines == "" || lines == "\n" || lines == " ")
{
this->endfile = true;
}
else
{
this->endfile = false;
}
this->data << lines;
this->data >> val;
}
return this->data;
}
MyStr & myFILE::operator >> (double& val)
{
string lines = "";
if (this->in != NULL)
{
if (!this->in->is_open())return this->data;
getline(*this->in, lines);
if (lines == "" || lines == "\n" || lines == " ")
{
this->endfile = true;
}
else
{
this->endfile = false;
}
this->data << lines;
this->data >> val;
}
return this->data;
}
MyStr & myFILE::operator >> (float& val)
{
string lines = "";
if (this->in != NULL)
{
if (!this->in->is_open())return this->data;
getline(*this->in, lines);
if (lines == "" || lines == "\n" || lines == " ")
{
this->endfile = true;
}
else
{
this->endfile = false;
}
this->data << lines;
this->data >> val;
}
return this->data;
}
MyStr & myFILE::operator >> (bool& val)
{
string lines = "";
if (this->in != NULL)
{
if (!this->in->is_open())return this->data;
getline(*this->in, lines);
if (lines == "" || lines == "\n" || lines == " ")
{
this->endfile = true;
}
else
{
this->endfile = false;
}
this->data << lines;
this->data >> val;
}
return this->data;
}
myFILE & myFILE::operator << (const string& s)
{
if (this->out != NULL)
{
if (!this->out->is_open())return *this;
*(this->out) << s << " ";
}
return *this;
}
myFILE & myFILE::operator << (const int& s)
{
if (this->out != NULL)
{
if (!this->out->is_open())return *this;
*(this->out) << s << " ";
}
return *this;
}
myFILE & myFILE::operator << (const unsigned int& s)
{
if (this->out != NULL)
{
if (!this->out->is_open())return *this;
*(this->out) << s << " ";
}
return *this;
}
myFILE & myFILE::operator << (const float& s)
{
if (this->out != NULL)
{
if (!this->out->is_open())return *this;
*(this->out) << s << " ";
}
return *this;
}
myFILE & myFILE::operator << (const double& s)
{
if (this->out != NULL)
{
if (!this->out->is_open())return *this;
*(this->out) << s << " ";
}
return *this;
}
myFILE & myFILE::operator << (const bool& s)
{
if (this->out != NULL)
{
if (!this->out->is_open())return *this;
*(this->out) << s << " ";
}
return *this;
}
myFILE & myFILE::operator << (const char& s)
{
if (this->out != NULL)
{
if (!this->out->is_open())return *this;
*(this->out) << s;
if (s != '\n')
*(this->out) << " ";
}
return *this;
}
void myFILE::init()
{
if ((type & ios_base::in) == (ios_base::in))
{
if (this->in != NULL)
if (this->in->is_open())
{
this->in->clear();
this->in->seekg(0, ios::beg);
}
}
else if ((type & ios_base::out) == (ios_base::out))
{
}
}