INI文件的读取
来源:互联网 发布:安卓版衣柜设计软件 编辑:程序博客网 时间:2024/04/26 15:06
//处理一般的配置文件 例如ini 文件
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
//使用vecotr 必须包含下面两句话
#include <vector>
using std::vector;
#include <map>
using std::map;
class IniFile{
public:
IniFile();
bool readinfile(const char * filepath);
void handlereadin();
private:
std::string filename;
vector<std::string> vecstring;
vector<std::string> parme;
map<string,string> strmap;
}; //分号是必须的
bool IniFile::readinfile(const char * filepath)
{
std::string str;
filename= new String(filepath);
ifstream infile(filename.c_str()); // 输入流绑定到文件
infile.open(filename.c_str());//打开文件,需传递c 风格字符串
if(!infile){
std::cerr<<"error : unable open input file!"<<std::endl;
return FALSE;
}
while(!infile.eof())
{
std::string str;
try
{
getline(infile,str); // str 不能声明在这里面,局部存储,处理异常时,将会不存在
}catch(expection &e)
{
std::cerr<<"exit with expection "<<e.what()<<std::endl;
return false;
}
vecstring.push_back(str);
}
return TRUE;
}
ifstream & IniFile::getreadstream(const char *path) //
{
// 流不允许复制,赋值等操作,也就不能存在于vector中也就
//不能做直接返回值或者形参,应传递其引用
ifstream infile(filename.c_str()); // 输入流绑定到文件
return infile;
}
void IniFile::handlereadin()
{
int first=0,last=0;
for(std::vector<string>::iterator it=vecstring.begin();it!=vecstring.end();it++)
{
first=*it.find('['));
last=*it.find(']');
if((first!=string::npos)&&(last!=string::npos)&&first!=last-1)
{
string s(*it.substr(first+1,last-1));
parme.push_back(s);
}
first=*it.find("//"));
if(first==string::npos)
{
last=*it.find('=');
string key=*it.substr(0,last-1);
string value=*it.substr(last+1,string::npos);
try{
strmap.insert(make_pair(key,value));
}catch(...) // 捕捉所有的异常
{
//throw;//抛弃异常
}
}
}
}
template <class T>
Handle<T>::Handle(T *p)
try:ptr(p),use(new size_t(1)) //构造函数初始化
{
}catch(const std::bad_alloc &e)
{
handle_out_of_memory(e);
}
{}
//派生异常类
class out_of_stock:: public std::runtime_error {
public :explicit out_of_stock(const std::string &s):std::runtime_error(s) {}
virtual ~out_of_stock() throw(){}
};
class isbn_mismatch:public std::logic_error{
public :explicit isbn_mismatch(const std:string &s):std:logic_error(s) {}
}
/////统计单词出现的次数
void sum()
{
map<string,int >words;
string word;
while(cin>>word)
{
++words[word]; //先查找是否有K=word的元素,没找到就将一个新的键值插入到words中来
} //set 单纯的键的组合,不支持下标操作与key_type相同的类型
}
void readmap()
{
map<string,int>word_count;
string word;
while(cin>>word)
{
pair<map<string,int>::iterator,bool> ret=word_count.insert(make_pair(word,1));
if(!ret.second)
++((ret.first)->second);
}
}
// k: map<k,v>::key_type ::键类型
// v: map<k,v>::mapped_type ::关联值类型
// word_count.insert(map<string,int>::value_type("key",value))
//word_count.insert(make_pair("key",value))
void search()
{
if(word_count.count("key_value")) // map 中只能一KEY 对应一实例,所以 count 返回的只能是1或0
occure=word_count["key_value"]; //这里 count ,[] 对value值做了2次操作
return occure;
map<string ,int>::iterator it=word_count.find("key_value");
if(it!=word_count.end()) // 没找到时,返回 end()
occure=it->second;
}
pair<K,V> & find_value(const map<K,V> & maps)
{
for(map<K,V>::iterator it=maps.begin(),it!=maps.end();it++)
{
if(it->first==""&&it->second->second=="")
{
return it;
}
}
for(map<K,V>::reverse_iterator r_it=maps.end();r_it!=maps.begin();r_it++)
{
// 从后向前迭代
}
}
void set()
{
set<int>iset; //set键只读
iset.insert(ivec.begin(),ivec.end()); // 返回 pair类型的对象(一个迭代器和一个bool值)迭代器指向拥有该键的元素,而bool表明是否添加了该元素
iset.find(1)//返回iterator 的引用 with key==1 或者end()
set<string> exclude;
string remove_word;
while(cin>>remove_word)
{
if(!exclude.count(remove_word))
++word_count[remove_word];// 不存在,则加入
}
}
- Ini文件的读取
- INI文件的读取
- INI文件的读取
- ini文件的读取
- ini文件的读取操作
- Java 读取 INI 文件的示例
- C读取ini文件的例子
- VB读取ini文件的函数
- 读取和写入Windows的INI文件
- 读取 INI 文件的模块儿
- MFC读取.ini文件的方法
- VC++对ini文件的读取
- ASP读取ini文件的实现方法
- Win32环境下的ini文件读取
- INI 文件的创建/修改/读取
- 读取保存密码的ini文件
- C++ ini文件的读取与写入
- MFC .ini文件的读取与写入
- 神勇的产品经理之路系列
- JavaRoadMap_3多线程
- INDEX FULL SCAN 与 INDEX FAST FULL SCAN
- 应该使用32位还是64位JVM
- 总监和经理的区别
- INI文件的读取
- 从ATMEL申请的AVR32样品
- 产品经理要懂多少技术?
- Qt中的焦点事件
- hibernate学习【新】
- java源码学习-----ArrayList
- 开源软件开发项目管理软件
- 在多线程下的long和double
- linux环境C++执行bash脚本