解析配置文件的一段练习代码

#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <iostream>
#include <vector>
#include <map>

using namespace std;

const string fileName = "config.txt";

map<string, string> tar_path;


int main()
{
	ifstream fin;
	fin.open(fileName);  
	if (false == fin.is_open()) {
		std::cerr << "open file failed!!" << std::endl;
		return -1;
	}
	string s;

	while (getline(fin, s))
	{
		size_t pos= s.find_first_of("=");
		if (pos == std::string::npos || pos+1>=s.size())
			continue;
		string targetName = s.substr(0,pos-1);
		string path = s.substr(pos+1);
		std::cout << targetName << " = " << path << std::endl;
	}
	fin.close();

	//================================================



    return 0;
}

  解析文本为        XXXX=E:\test\1234

posted on 2018-07-20 17:57  itdef  阅读(144)  评论(0编辑  收藏  举报

导航