c++ 实现对文件的读取和查找(写暂时没写)
来源:互联网 发布:房地产网络推广职责 编辑:程序博客网 时间:2024/04/19 01:38
CPmap.h
#pragma warning(disable: 4786) // vc中去除STL使用带来的警告
#ifndef __CFGWRITER_H__ //防止重复调用头文件
#define __CFGWRITER_H__
#include "StdAfx.h"
#include <string>
#include <map>
#include <iterator>
#include <iostream>
using namespace std;
typedef map<string,string,less<string> > Mmap;//
typedef Mmap::iterator MIt; //
class CPmap
{
public:
CPmap();
CPmap(string mpath);
Mmap public_map;
//MIt It;
virtual ~CPmap();
void show();
private:
string path;
bool do_open(const char* mpath);
};
#endif // !defined(AFX_PMAP_H__1A8D3C1B_6420_4A67_A5EB_C4CE390B2925__INCLUDED_)
CPmap.cpp
// Pmap.cpp: implementation of the CPmap class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Pmap.h"
#include "Write.h"
#include <fstream>
#include <vector>
#include <algorithm> //use zhe for_each()
#define LEFT [
#define RIGHT ]
#define NOTES #
#define EQUAL =
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const char* const MIDDLE = "***";
CPmap::CPmap()
{
cout<<"don't find File"<<endl;
}
CPmap::CPmap(string mpath)
{
this->path=mpath;
if(do_open(mpath.c_str()))
cout<<"open the File: ok"<<endl;
else
cout<<"open the File:Error"<<endl;
}
CPmap::~CPmap()
{
}
bool CPmap::do_open(const char* mpath)
{
ifstream fin(mpath);
if (!fin.is_open())
return false;
vector<string> strvect;
vector<string>::iterator it;
string buffer;
while (!fin.eof())
{
getline(fin,buffer,'\n');
strvect.push_back(buffer);
}
it=strvect.begin();
if(strvect.empty())
return false;
buffer="";
string session;
while(it!=strvect.end())
{
//---------------------------
buffer=(*it);
int first=buffer.find('[');
int last=buffer.find(']');
if(first!=string::npos&&last!=string::npos&&first!=last+1)
{
cout<<(*it)<<endl;
session=buffer.substr(first+1,last-first-1);
it++;
continue;
}
if(session.empty())
{ it++;
continue;
}
else
cout<<(*it)<<endl;//
if((first=buffer.find('='))==string::npos)
{
it++;
continue;
}
string strtmp1=buffer.substr(0,first);
string strtmp2=buffer.substr(first+1,string::npos);
first = strtmp1.find_first_not_of(" \t");
last = strtmp1.find_last_not_of(" \t");
if (first==string::npos||last==string::npos)
{
it++;
continue;
}
string strkey=strtmp1.substr(first,last-first+1);
first=strtmp2.find_first_not_of(" \t");
if(((last=strtmp2.find("\t#",first))!=string::npos)||
((last=strtmp2.find(" #",first))!=string::npos))
strtmp2=strtmp2.substr(0,last-first);
last = strtmp2.find_last_not_of(" \t");
if (first==string::npos||last==string::npos)
{
it++;
continue;
}
string strvalue=strtmp2.substr(first,last-first+1);
string mapkey = session+MIDDLE;
mapkey+=strkey;
public_map[mapkey]=strvalue;
//----------------------------
it++;
}
return true;
}
void CPmap::show()
{
cout<<"ok"<<endl;
}
写操作
// Write.cpp: implementation of the CWrite class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Write.h"
#include "Pmap.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class CPmap;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWrite::CWrite() //default Constructor
{
cout<<"Sorry,you maybe before put the path"<<endl;
return;
}
CWrite::CWrite(CPmap cpmap) //Constructor
{
this->pmap=cpmap;
}
CWrite::~CWrite() //
{
}
void CWrite::show(string path) //realize the class
{
this->wpath=path;
fstream file;
file.open(wpath.c_str());
int do_mark;
vector<string>::iterator it=pmap.strvect.begin();
string str="";
cout<<"1,Add to a session\n"<<"2,Add to existing a new session\n"
<<"Please input 0 exit\n"
<<"what are you doing?:";
while(str=="")
{
cin>>do_mark;
cout<<endl;
switch(do_mark)
{
case 1:
cout<<"Please input a name of the session:";
cin>>str;
str="["+str+"]";
while(it!=pmap.strvect.end())
{
if((*it).find(str)!=string::npos)
{
it++;
if((*it).find('=')!=string::npos)
it++;
cout<<"Please input the key and value(a=b):";
cin>>str;
pmap.strvect.insert(it,str);
file.clear();
file.close();
file.open(wpath.c_str(),ios::out|ios::trunc);
it=pmap.strvect.begin();
for (int i=0;i<pmap.strvect.size();i++)
{
file<<pmap.strvect[i]<<endl;
}
break;
}
it++;
}
//pmap.public_map
break;
case 2:
cout<<"Please input the new session:";
cin>>str;
str="["+str+"]";
file.seekg(0,ios::end);
file<<"\n\n"+str;
while(str!="exit")
{
cout<<"Please input the key and value(a=b)(input 'exit' to exit):";
cin>>str;
if(str.find('=')!=string::npos)
{
file.seekg(0,ios::end);
file<<"\n"+str;
cout<<"insert success"<<endl;
}
}
file.close();
break;
case 0:
return;
default:
cout<<"Error!please input again!"<<endl;
cout<<"1,Add to a session\n"<<"2,Add to existing a new session\n"
<<"Please input 0 exit\n"
<<"what are you doing?:";
break;
}
}
}
void CWrite::pack(string str)
{
string session=str;
}
void CWrite::read(string wpath)
{
}
- c++ 实现对文件的读取和查找(写暂时没写)
- ~~暂时没得写!~~
- java源码——对文件内容的查找和替换(开始写界面咯)
- 写文件和查找文件
- 暂时没什么好写的
- 纯C++写的类似于VC对INI文件的读写操作(关于注释的暂时没有处理)
- 文件读取和写入时,文件分隔符的差别
- 最近比较忙,暂时没时间写树了
- 从链表中读取文件和写文件
- 今天写了个关于大整数的保存结构和实现大整数相加的代码(暂时仅限正整数)
- jxl包实现对Excel文件的写读写操作
- 【学习C++】C++项目的头文件和实现文件分别写什么
- MFC中读取和写ini文件
- MFC读取和写ini文件
- 纯C语言写的折半查找
- C写的 xml解析器,对xml进行读写查找...【博主没有验证的...】
- Java实现对Excel数据的读和写
- 写文件时字段对齐和填充的C代码实现
- 出现javax.servlet.ServletException: BeanUtils.populate异常
- 创建list(列表)分区!
- 学车总结
- 使IE6下PNG背景透明的方法
- STM32 GPIO 相关寄存器
- c++ 实现对文件的读取和查找(写暂时没写)
- js 得到上个月第一天和最后一天
- 精彩C#
- 第五章 类和继承
- 基于Linux操作系统下运行命令时CTRL+Z的作用
- ps颜色不正确 偏色 失真
- js/css文件的优化--转自IBM-developerworks
- 【原】屏蔽免费空间的谷歌广告
- 汉诺塔算法