以行为单位的文件处理程序
2005-03-01 21:29
288 查看
有些应用中,不一定必须要数据库支持,纪录可以保存在文本里,此时,
如何对这些记录进行增删改查的操作呢?这里以C++标准库提供了一个类,
来处理此类问题。该类以行为单位进行操作,设一条纪录存为一行。只要
拿到一行数据,再用string类进行其他处理也就不费事了。
头文件:
/*************************************************************
* LineFile class Interface
*
* [author] iori.ogami
* [date] 2005-1-15
* [ver] 1.3
* [env] Visual C++ 6.0 sp5
*
**************************************************************/
#pragma warning(disable:4786)
#include <string>
#include <vector>
#include <fstream>
class LineFile
{
public:
LineFile(){};
LineFile(std::string fileName);
LineFile(const LineFile& lf);
LineFile& operator =(const LineFile& lf);
virtual ~LineFile();
long GetLineCount() const { return ln_cnt; }
std::string GetLineText(long lnnum) const;
void UpdateLine(long lnnum, std::string txt);
void InsertLine(long lnnum, std::string txt);
void DeleteLine(long lnnum);
void AddLine(std::string txt);
long GetLineByKeyWord(std::string keyWord) const;
std::vector<long> GetLinesByKeyWord(std::string keyWord) const;
protected:
std::fstream* pFile;
std::vector<std::string> vec;
std::string file_name;
long ln_cnt;
private:
bool Reset();
void dump();
};
CPP文件:
/************************************
implementation of LineFile class
*************************************/
#include "LineFile.h"
#include <iostream>
#include <ios>
#define MAX_CHAR 1024 // max chars in a line, define your own MAX_CHAR here
using namespace std;
/*
* constructor
*/
LineFile::LineFile(std::string fileName): file_name(fileName),ln_cnt(0)
{
pFile = new fstream( file_name.c_str(), ios_base::out|ios_base::app ); // create if not found
pFile->close();
pFile->open(file_name.c_str(), ios_base::in | ios_base::out); // reopen for read and write
char s[MAX_CHAR] = {0};
string ss;
while( pFile->getline(s,MAX_CHAR) )
{
cout<<s<<endl;
ss = s;
vec.push_back(ss);
}
ln_cnt = vec.size();
pFile->clear();
pFile->seekg( ios_base::beg );
cout<<"construct end. line number: "<<ln_cnt<<endl;
}
/*
* copy constructor
*/
LineFile::LineFile(const LineFile& lf):
file_name(lf.file_name),
vec(lf.vec),
ln_cnt(lf.ln_cnt)
{
pFile = new fstream( lf.file_name.c_str(), ios_base::in | ios_base::out );
}
/*
* the destructor
*/
LineFile::~LineFile()
{
if(pFile)
{
if(pFile->is_open()) pFile->close();
delete pFile;
}
}
/*
* move the stream poiter to the start position. if the stream status is 'fail', recover it.
*/
bool LineFile::Reset()
{
if( pFile->bad() )
{
cout<<"[LineFile::Reset] bad stream"<<endl;
return false;
}
if( pFile->fail() ) pFile->clear();
pFile->seekg( ios_base::beg );
return true;
}
/*
* save the file text from vector to the file stream.
*/
void LineFile::dump()
{
// reopen and trunk the file
Reset();
pFile->close();
pFile->open(file_name.c_str(), ios_base::out | ios_base::trunc);
ln_cnt = vec.size();
for(long i=0; i<ln_cnt; i++)
{
string s = vec[i];
*pFile<<s.c_str()<<endl;
}
pFile->close();
pFile->open( file_name.c_str(), ios_base::in | ios_base::out );
}
/*
* evaluate operator '='
*/
LineFile& LineFile::operator =(const LineFile& lf)
{
file_name = lf.file_name;
ln_cnt = lf.ln_cnt;
if( !vec.empty() ) vec.clear();
vec = lf.vec;
if(pFile)
{
if(pFile->is_open()) pFile->close();
delete pFile;
}
pFile = new fstream( lf.file_name.c_str(), ios_base::in | ios_base::out );
return *this;
}
/******************************/
// the interfaces
/******************************/
/*
* get a string by line number,note that the feedline character '/n' is not included.
*/
string LineFile::GetLineText(long lnnum) const
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::GetLineText] illegal line number: "<<lnnum<<endl;
return "";
}
return vec[lnnum-1];
}
/*
* insert a line at lnnum, and append a feedline character.
*/
void LineFile::InsertLine(long lnnum, std::string txt)
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::InsertLine] illegal line number: "<<lnnum<<endl;
return;
}
vector<string>::iterator iter = vec.begin();
for(long i=0; i<lnnum-1; i++,iter++);
vec.insert(iter, txt);
dump();
}
/*
* update the specifized line with a new string, also append a feedline character.
*/
void LineFile::UpdateLine(long lnnum, std::string txt)
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::UpdateLine] illegal line number: "<<lnnum<<endl;
return;
}
vec[lnnum-1] = txt;
dump();
}
/*
* delete a line
*/
void LineFile::DeleteLine(long lnnum)
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::DeleteLine] illegal line number: "<<lnnum<<endl;
return;
}
vector<string>::iterator iter = vec.begin();
for(int i=0; i<lnnum-1; i++,iter++);
vec.erase(iter);
dump();
}
/*
* append a line at the end of the file.
*/
void LineFile::AddLine(std::string txt)
{
vec.push_back(txt);
dump();
}
/*
* get the line number by a key word, return 0 if not found.
*/
long LineFile::GetLineByKeyWord(string keyWord) const
{
for(long i=0; i<ln_cnt; i++)
{
if( vec.at(i).find(keyWord)!=-1 )
return i+1;
}
return 0;
}
/*
* get the line numbers by a key word.
*/
vector<long> LineFile::GetLinesByKeyWord(string keyWord) const
{
vector<long> v;
for(long i=0; i<ln_cnt; i++)
{
if( vec.at(i).find(keyWord)!=-1 )
v.push_back(i+1);
}
return v;
}
如何对这些记录进行增删改查的操作呢?这里以C++标准库提供了一个类,
来处理此类问题。该类以行为单位进行操作,设一条纪录存为一行。只要
拿到一行数据,再用string类进行其他处理也就不费事了。
头文件:
/*************************************************************
* LineFile class Interface
*
* [author] iori.ogami
* [date] 2005-1-15
* [ver] 1.3
* [env] Visual C++ 6.0 sp5
*
**************************************************************/
#pragma warning(disable:4786)
#include <string>
#include <vector>
#include <fstream>
class LineFile
{
public:
LineFile(){};
LineFile(std::string fileName);
LineFile(const LineFile& lf);
LineFile& operator =(const LineFile& lf);
virtual ~LineFile();
long GetLineCount() const { return ln_cnt; }
std::string GetLineText(long lnnum) const;
void UpdateLine(long lnnum, std::string txt);
void InsertLine(long lnnum, std::string txt);
void DeleteLine(long lnnum);
void AddLine(std::string txt);
long GetLineByKeyWord(std::string keyWord) const;
std::vector<long> GetLinesByKeyWord(std::string keyWord) const;
protected:
std::fstream* pFile;
std::vector<std::string> vec;
std::string file_name;
long ln_cnt;
private:
bool Reset();
void dump();
};
CPP文件:
/************************************
implementation of LineFile class
*************************************/
#include "LineFile.h"
#include <iostream>
#include <ios>
#define MAX_CHAR 1024 // max chars in a line, define your own MAX_CHAR here
using namespace std;
/*
* constructor
*/
LineFile::LineFile(std::string fileName): file_name(fileName),ln_cnt(0)
{
pFile = new fstream( file_name.c_str(), ios_base::out|ios_base::app ); // create if not found
pFile->close();
pFile->open(file_name.c_str(), ios_base::in | ios_base::out); // reopen for read and write
char s[MAX_CHAR] = {0};
string ss;
while( pFile->getline(s,MAX_CHAR) )
{
cout<<s<<endl;
ss = s;
vec.push_back(ss);
}
ln_cnt = vec.size();
pFile->clear();
pFile->seekg( ios_base::beg );
cout<<"construct end. line number: "<<ln_cnt<<endl;
}
/*
* copy constructor
*/
LineFile::LineFile(const LineFile& lf):
file_name(lf.file_name),
vec(lf.vec),
ln_cnt(lf.ln_cnt)
{
pFile = new fstream( lf.file_name.c_str(), ios_base::in | ios_base::out );
}
/*
* the destructor
*/
LineFile::~LineFile()
{
if(pFile)
{
if(pFile->is_open()) pFile->close();
delete pFile;
}
}
/*
* move the stream poiter to the start position. if the stream status is 'fail', recover it.
*/
bool LineFile::Reset()
{
if( pFile->bad() )
{
cout<<"[LineFile::Reset] bad stream"<<endl;
return false;
}
if( pFile->fail() ) pFile->clear();
pFile->seekg( ios_base::beg );
return true;
}
/*
* save the file text from vector to the file stream.
*/
void LineFile::dump()
{
// reopen and trunk the file
Reset();
pFile->close();
pFile->open(file_name.c_str(), ios_base::out | ios_base::trunc);
ln_cnt = vec.size();
for(long i=0; i<ln_cnt; i++)
{
string s = vec[i];
*pFile<<s.c_str()<<endl;
}
pFile->close();
pFile->open( file_name.c_str(), ios_base::in | ios_base::out );
}
/*
* evaluate operator '='
*/
LineFile& LineFile::operator =(const LineFile& lf)
{
file_name = lf.file_name;
ln_cnt = lf.ln_cnt;
if( !vec.empty() ) vec.clear();
vec = lf.vec;
if(pFile)
{
if(pFile->is_open()) pFile->close();
delete pFile;
}
pFile = new fstream( lf.file_name.c_str(), ios_base::in | ios_base::out );
return *this;
}
/******************************/
// the interfaces
/******************************/
/*
* get a string by line number,note that the feedline character '/n' is not included.
*/
string LineFile::GetLineText(long lnnum) const
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::GetLineText] illegal line number: "<<lnnum<<endl;
return "";
}
return vec[lnnum-1];
}
/*
* insert a line at lnnum, and append a feedline character.
*/
void LineFile::InsertLine(long lnnum, std::string txt)
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::InsertLine] illegal line number: "<<lnnum<<endl;
return;
}
vector<string>::iterator iter = vec.begin();
for(long i=0; i<lnnum-1; i++,iter++);
vec.insert(iter, txt);
dump();
}
/*
* update the specifized line with a new string, also append a feedline character.
*/
void LineFile::UpdateLine(long lnnum, std::string txt)
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::UpdateLine] illegal line number: "<<lnnum<<endl;
return;
}
vec[lnnum-1] = txt;
dump();
}
/*
* delete a line
*/
void LineFile::DeleteLine(long lnnum)
{
if( lnnum >ln_cnt || lnnum<=0 )
{
cout<<"[LineFile::DeleteLine] illegal line number: "<<lnnum<<endl;
return;
}
vector<string>::iterator iter = vec.begin();
for(int i=0; i<lnnum-1; i++,iter++);
vec.erase(iter);
dump();
}
/*
* append a line at the end of the file.
*/
void LineFile::AddLine(std::string txt)
{
vec.push_back(txt);
dump();
}
/*
* get the line number by a key word, return 0 if not found.
*/
long LineFile::GetLineByKeyWord(string keyWord) const
{
for(long i=0; i<ln_cnt; i++)
{
if( vec.at(i).find(keyWord)!=-1 )
return i+1;
}
return 0;
}
/*
* get the line numbers by a key word.
*/
vector<long> LineFile::GetLinesByKeyWord(string keyWord) const
{
vector<long> v;
for(long i=0; i<ln_cnt; i++)
{
if( vec.at(i).find(keyWord)!=-1 )
v.push_back(i+1);
}
return v;
}
相关文章推荐
- java对txt文本文件的复制粘贴(对文件内容进行了简单的修改)以行为一个处理单位
- Windows Mobile 启动自带程序处理文件
- 文件上传 下载 一般处理程序和webForm联系以及asp.net内置对象
- HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。
- 解决问题:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
- 实战 HTTP 处理程序(HTTP Handler) (5)---不用临时文件,直接打开动态生成的文件
- WCF HTTP 错误 404.3 - Not Found(由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。)
- sql2000和vs.net2003安装程序时处理文件挂起的问题
- HTTP错误 404.17 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
- 一个非常非常非常基础的程序,写的不好,但是一般的文件读写操作及字符处理函数都涉及到了..新手学习用的
- 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中
- 简单的bash script,实现以行为单位读入文件,并打印出某一列
- Errore HTTP 404.2 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
- 【工具类】JAVA 以行为单位读取文件并比对
- svc 报“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。“的HTTP 错误 404.3 – Not Found
- 通过一般处理程序实现【文件上传】
- 404.17 - 动态内容通过通配符 MIME 映射映射到静态文件处理程序
- 关于ASP.NET 中 Global.asax 文件的后台事件处理程序
- 一个在DOS下处理中文和长名字目录和文件的程序(原创)
- asp.net 一般处理程序接收上传文件的问题