说明
在之前的txt文件流读写程序的基础上进行改进,简化文件数据读取的写入的流程,适用于文件的配置参数的读写,保证文件整体读写的便捷性和速度。不宜用于文件单点位置的参数修改。
/**********************************************
Description:模板类读取文件
主要函数:void modifyName(string name); 作用:修改默认文件的名称
vector<vecotr<T>> read(); 作用:读取txt文件中的数据,并返回一个数组
bool CTxtT<T>::write(vector<vector<T>> vec); 作用:向txt文件中写入一个数组
Other: 文件使用时cpp文件放到该件所在的目录下,但不能添加到项目中
数据间采用空格进行分割
该函数较适用于文件的一次性读写,适合加载或保存配置参数使用,二维情况下适合数组数组存储
使用例子: CTxtT<double> file(fileName); //创建,要确保文本文件中的数据格式是相同的
vec=file.read(); //读
>> //读
file.write(vec); //写
<< //写
History
<author> <time> <veersion> <desc>
*********************************************/
#ifndef CTXTT_H
#define CTXTT_H
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
typedef struct { //文本文件的行列数
int colCount, rowCount; //行数和列数
}txtSize;
template <class T>
class CTxtT
{
public:
public:CTxtT(string name);
void modifyName(string name);
~CTxtT() { }
void operator<<(const T & src);
void operator<<(const vector<T> & vec);
void operator<<(const vector<vector<T>> & vec);
void operator>>( T & src);
void operator>>( vector<T> & vec);
void operator>>( vector<vector<T>> & vec);
private:
int split(const string &str, const string &pattern);
vector<vector<T>> read();
bool write(vector<vector<T>> vec);
txtSize gettxtSize(); //获取行数和列数
string name;
};
template<class T>
CTxtT<T>::CTxtT(string name)
{
this->name = name;
}
template<class T>
void CTxtT<T>::modifyName(string name)
{
this->name = name;
}
template<class T>
vector<vector<T>> CTxtT<T>::read()
{
ifstream myfile(name.c_str());
if (!myfile.is_open())
{
return { };
}
txtSize size= gettxtSize();
int w, h;
w = size.colCount;
h = size.rowCount;
vector<vector<T>> vec;
vec.resize(h);
for (int i = 0; i < h; i++)
{
vec[i].resize(w);
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
myfile >> vec[i][j];
}
}
myfile.close();
return vec;
}
template<class T>
bool CTxtT<T>::write(vector<vector<T>> vec)
{
if (vec.empty()) //空的,不做任何处理
return false;
if (name.empty())
return false;
unsigned int h = vec.size();
unsigned int w = vec[0].size();
ofstream outfile(name.c_str());
if (!outfile.is_open())
return false;
outfile.clear();
//outfile << w << " " << h << "\n";
for (unsigned int i = 0; i < h; i++)
{
for (unsigned int j = 0; j < w; j++)
{
outfile << vec[i][j];
if (j != h - 1)
outfile << " ";
}
outfile << "\n";
}
outfile.close();
return true;
}
template<class T>
txtSize CTxtT<T>::gettxtSize()
{
ifstream readFile;
int m = 0, n = 0;
string tmp;
readFile.open(name, ios::in);
if (readFile.fail())
{
return { };
}
else
{
while (getline(readFile, tmp))
{
if (n == 0)
{
m = split(tmp, " ");
}
n++;
}
}
txtSize size;
size.rowCount = n;
size.colCount = m;
return size;
}
template<class T>
void CTxtT<T>::operator<<(const T &src)
{
vector<vector<T>> vec2;
vector<T> vec1;
vec1.push_back(src);
vec2.push_back(vec1);
write(vec2);
}
template<class T>
void CTxtT<T>::operator<<(const vector<T> &vec)
{
vector<vector<T>> vec2;
vec2.push_back(vec);
write(vec2);
}
template<class T>
void CTxtT<T>::operator<<(const vector<vector<T> > &vec)
{
write(vec);
}
template<class T>
void CTxtT<T>::operator>>( T &src)
{
vector<vector<T>> vec2;
vec2=read();
if(vec2.empty())
{
return;
}
T n=vec2[0][0];
src=n;
}
template<class T>
void CTxtT<T>::operator>>( vector<T> &vec)
{
vector<vector<T>> vec2;
vec2=read();
if(vec2.empty())
{
return ;
}
vec=vec2.at(0);
}
template<class T>
void CTxtT<T>::operator>>(vector<vector<T> > &vec)
{
vec=read();
}
template<class T>
int CTxtT<T>::split(const string &str, const string &pattern)
{
vector<string> res;
if (str == "")
return 0;
//在字符串末尾也加入分隔符,方便截取最后一段
string strs = str + pattern;
size_t pos = strs.find(pattern);
while (pos != strs.npos)
{
string temp = strs.substr(0, pos);
if (temp != ""&&temp!=" ")
{
res.push_back(temp);
}
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos + 1, strs.size());
pos = strs.find(pattern);
}
return res.size();
}
#endif
使用范例
如文件中只存储一个参数。
CTxtT<string> mytxt("language.txt");
string language;
mytxt>>language;
如文件中只存储一行数据。
CTxtT<double> mytxt("data.txt");
vector<double> data;
mytxt>>data;
如文件存储一个二维数组,二维数组应确保文件中的所有参数不为空或者空格,结构混乱。
CTxtT<double> mytxt("data2.txt");
vector<vector<int>> data;
mytxt>>data;