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)
{

}




原创粉丝点击