有个朋友问了我一些关于文件读写的问题。其实我比较恨文件读写,我在文件读写上经常出现一些问题。所以这次狠下心决定写一个通用模板类,处理一些常见的问题。代码如下
template <class T>
class CFileOperation
{
private:
vector<T> m_vecElements;
public:
CFileOperation();
~CFileOperation();
/*第二个参数是一个函数指针,用来作为处理每一个已读字符串,可以通过该函数将字符串转换成需要的类型T,或者进行一些其他的处理。第四个参数是选择需要保留之前已经读取的数据。*/
bool ReadFromFile(char * filename, \
T (*dealElement)(char * , void *), void * param = NULL,\
bool bErase = false);
/*第二个参数是间隔,用于间隔两个数据。第三个参数是一个函数指针,用来作为处理每一个需要写进文件的T类型数据,可以通过该函数加工T类型数据。*/
bool WriteIntoFile(char * filename, vector<T> & arrayOut, char * interval,\
T (*dealElement)(T& , void *) = NULL , void * param = NULL);
bool GetElement(int index, T * result);
vector<T> GetWholeVector();
};
template <class T>
vector<T> CFileOperation<T>::GetWholeVector()
{
return m_vecElements;
}
template <class T>
bool CFileOperation<T>::GetElement(int index, T * pResult)
{
if (m_vecElements.size() == 0 || index >= m_vecElements.size())
{
cerr << "the size you entered is longer than the vector\n";
pResult = NULL;
return false;
}
*pResult = m_vecElements[index];
return true;
}
template <class T>
bool CFileOperation<T>::WriteIntoFile(char * filename, vector<T> & arrayOut,char * interval,T (*dealElement)(T & , void *),void * param)
{
if (filename == NULL)
{
return false;
}
ofstream outfile(filename, ios_base::trunc);
if (!outfile)
{
cerr << "error in open the file";
return false;
}
int i = 0;
while(i < arrayOut.size())
{
T t = arrayOut[i];
if (dealElement != NULL)
{
t = dealElement(arrayOut[i], param);
}
outfile << t;
outfile << interval;
i++;
}
outfile.close();
return true;
}
template <class T>
bool CFileOperation<T>::ReadFromFile(char * filename, T (*dealElement)(char * , void *) ,
void * param,
bool bErase)
{
//将先前的内容清楚
if (filename == NULL)
{
return false;
}
ifstream infile(filename);
if (infile.fail())
{
cerr << "error in open the file";
return false;
}
if (bErase)
{
m_vecElements.clear();
}
while(1)
{
char temp[100];
infile >> temp;
T t = dealElement(temp, param);
m_vecElements.push_back(t);
if (infile.eof())
{
break;
}
}
infile.close();
return true;
}
template <class T>
CFileOperation<T>::~CFileOperation()
{
}
template <class T>
CFileOperation<T>::CFileOperation()
{
}
class CFileOperation
{
private:
vector<T> m_vecElements;
public:
CFileOperation();
~CFileOperation();
/*第二个参数是一个函数指针,用来作为处理每一个已读字符串,可以通过该函数将字符串转换成需要的类型T,或者进行一些其他的处理。第四个参数是选择需要保留之前已经读取的数据。*/
bool ReadFromFile(char * filename, \
T (*dealElement)(char * , void *), void * param = NULL,\
bool bErase = false);
/*第二个参数是间隔,用于间隔两个数据。第三个参数是一个函数指针,用来作为处理每一个需要写进文件的T类型数据,可以通过该函数加工T类型数据。*/
bool WriteIntoFile(char * filename, vector<T> & arrayOut, char * interval,\
T (*dealElement)(T& , void *) = NULL , void * param = NULL);
bool GetElement(int index, T * result);
vector<T> GetWholeVector();
};
template <class T>
vector<T> CFileOperation<T>::GetWholeVector()
{
return m_vecElements;
}
template <class T>
bool CFileOperation<T>::GetElement(int index, T * pResult)
{
if (m_vecElements.size() == 0 || index >= m_vecElements.size())
{
cerr << "the size you entered is longer than the vector\n";
pResult = NULL;
return false;
}
*pResult = m_vecElements[index];
return true;
}
template <class T>
bool CFileOperation<T>::WriteIntoFile(char * filename, vector<T> & arrayOut,char * interval,T (*dealElement)(T & , void *),void * param)
{
if (filename == NULL)
{
return false;
}
ofstream outfile(filename, ios_base::trunc);
if (!outfile)
{
cerr << "error in open the file";
return false;
}
int i = 0;
while(i < arrayOut.size())
{
T t = arrayOut[i];
if (dealElement != NULL)
{
t = dealElement(arrayOut[i], param);
}
outfile << t;
outfile << interval;
i++;
}
outfile.close();
return true;
}
template <class T>
bool CFileOperation<T>::ReadFromFile(char * filename, T (*dealElement)(char * , void *) ,
void * param,
bool bErase)
{
//将先前的内容清楚
if (filename == NULL)
{
return false;
}
ifstream infile(filename);
if (infile.fail())
{
cerr << "error in open the file";
return false;
}
if (bErase)
{
m_vecElements.clear();
}
while(1)
{
char temp[100];
infile >> temp;
T t = dealElement(temp, param);
m_vecElements.push_back(t);
if (infile.eof())
{
break;
}
}
infile.close();
return true;
}
template <class T>
CFileOperation<T>::~CFileOperation()
{
}
template <class T>
CFileOperation<T>::CFileOperation()
{
}
测试使用的代码
代码
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
#include "FileOperation.h"
using namespace std;
double DealDouble(char * charactor, void * param)
{
return ( atof(charactor) + 1);
}
string DealString(char * charactor, void * param)
{
string str(charactor);
return str.append("111111");
}
string BeforWriting(string & charactor, void * param)
{
return charactor.append("111111");
}
int _tmain(int argc, _TCHAR* argv[])
{
CFileOperation<double> fileOperation;
fileOperation.ReadFromFile("d:\\1.txt", DealDouble, NULL, false);
vector<double> vec = fileOperation.GetWholeVector();
for (int i = 0;i < vec.size();i++)
{
cout << vec[i] << endl;
}
double d;
if( fileOperation.GetElement(3, &d) )
cout << "the third element is : " << d << endl;
//append some some other number and then to write into files
for (int i = 0; i < 5; i++)
{
vec.push_back(i + 50.512345);
}
fileOperation.WriteIntoFile("d:\\3.txt", vec, "\n");
//////////////////////////////////////////////////////
CFileOperation<string> fileOperation2;
fileOperation2.ReadFromFile("d:\\2.txt", DealString, NULL, false);
vector<string> vec2 = fileOperation2.GetWholeVector();
for (int i = 0;i < vec2.size();i++)
{
cout << vec2[i] << endl;
}
string d2;
if( fileOperation2.GetElement(3, &d2) )
cout << "the third element is : " << d2 << endl;
//append some some other number and then to write into files
for (int i = 0; i < 5; i++)
{
string str("456231456");
vec2.push_back(str);
}
fileOperation2.WriteIntoFile("d:\\4.txt", vec2, "\n", BeforWriting);
return 0;
}
#include <stdlib.h>
#include <iostream>
#include <vector>
#include "FileOperation.h"
using namespace std;
double DealDouble(char * charactor, void * param)
{
return ( atof(charactor) + 1);
}
string DealString(char * charactor, void * param)
{
string str(charactor);
return str.append("111111");
}
string BeforWriting(string & charactor, void * param)
{
return charactor.append("111111");
}
int _tmain(int argc, _TCHAR* argv[])
{
CFileOperation<double> fileOperation;
fileOperation.ReadFromFile("d:\\1.txt", DealDouble, NULL, false);
vector<double> vec = fileOperation.GetWholeVector();
for (int i = 0;i < vec.size();i++)
{
cout << vec[i] << endl;
}
double d;
if( fileOperation.GetElement(3, &d) )
cout << "the third element is : " << d << endl;
//append some some other number and then to write into files
for (int i = 0; i < 5; i++)
{
vec.push_back(i + 50.512345);
}
fileOperation.WriteIntoFile("d:\\3.txt", vec, "\n");
//////////////////////////////////////////////////////
CFileOperation<string> fileOperation2;
fileOperation2.ReadFromFile("d:\\2.txt", DealString, NULL, false);
vector<string> vec2 = fileOperation2.GetWholeVector();
for (int i = 0;i < vec2.size();i++)
{
cout << vec2[i] << endl;
}
string d2;
if( fileOperation2.GetElement(3, &d2) )
cout << "the third element is : " << d2 << endl;
//append some some other number and then to write into files
for (int i = 0; i < 5; i++)
{
string str("456231456");
vec2.push_back(str);
}
fileOperation2.WriteIntoFile("d:\\4.txt", vec2, "\n", BeforWriting);
return 0;
}