#include "stdafx.h"
#include <fstream>
#include <string>
#include <iostream>
bool ParseLine(const std::string& line, std::string& key, std::string& value);
class Demo
{
public:
typedef struct _Data
{
std::string name;
std::string email;
} Data;
Data data;
};
int main()
{
std::ifstream ifs("d:\\demo.txt");
std::string line;
std::string key;
std::string value;
Demo obj;
while(!ifs.eof())
{
std::getline(ifs, line);
if (ParseLine(line, key, value))
{
if (0 == key.compare("name"))
{
obj.data.name = value;
}
else if (0 == key.compare("email"))
{
obj.data.email = value;
}
else
{
std::cerr<<"Unknow key:" << key << std::endl;
}
}
}
return 0;
}
bool ParseLine(const std::string& line, std::string& key, std::string& value)
{
bool flag = false;
if (line.find("//") != 0)
{
size_t pos = line.find("=");
if (pos != std::string::npos)
{
key = line.substr(0, pos);
value = line.substr(pos + 1, line.length());
flag = true;
}
}
return flag;
}
</iostream></string></fstream>
demo.txt:
name=jhon
email=icerlion@163.com