C++ 读取配置文件
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
#include<Windows.h>
#include<list>
#include<vector>
#include<sstream>
using namespace std;
struct Account
{
string MDUrl;
string TradeUrl;
string userName;
string password;
};
struct TaoliSetting
{
string leg1;
string leg2;
string endTime;
string entryPrice;
string step;
string maxOrder;
string minOrder;
};
struct DanbianSetting
{
string inst;
string entryPrice;
string step;
string maxOrder;
string minOrder;
};
struct TBSetting
{
string TBUrl;
};
string getPassword(string pwd)
{
for (int i = 0; i < pwd.size(); i++)
{
char a = pwd[i];
if (a > '0' && a <= '9' || (a > 'A' && a <= 'Z') || (a > 'a' && a <= 'z'))
{
a = (char)(a - 1);
}
else if (a == '0')
{
a = '9';
}
else if (a == 'A')
{
a = 'Z';
}
else if (a == 'a')
{
a = 'z';
}
pwd[i] = a;
}
return pwd;
}
void getAccountInfo()
{
Account account;
TaoliSetting taoli;
DanbianSetting danbian;
TBSetting TB;
char buffer[256];
vector<string> str;
ifstream in("E:\\Projects\\C++Application\\CTPtrader\\CTPtrader\\bin\\Debug\\account.txt");
if (! in.is_open())
{
cout << "Error opening file";
return;
}
while (!in.eof() )
{
in.getline (buffer,200);
str.push_back(buffer);
}
account.MDUrl=str[0].substr(str[0].find(' ')+1);
account.TradeUrl=str[1].substr(str[1].find(' ')+1);
account.userName=str[2].substr(str[2].find(' ')+1);
account.password=getPassword(str[3].substr(str[3].find(' ')+1));
string type=str[4].substr(str[4].find(' ')+1);
if(type=="套利")
{
taoli.leg1=str[5].substr(str[5].find(' ')+1);
taoli.leg2=str[6].substr(str[6].find(' ')+1);
taoli.endTime=str[7].substr(str[7].find(' ')+1);
taoli.entryPrice=str[8].substr(str[8].find(' ')+1);
taoli.step=str[9].substr(str[9].find(' ')+1);;
taoli.maxOrder=str[10].substr(str[10].find(' ')+1);
taoli.minOrder=str[11].substr(str[11].find(' ')+1);
}
else if(type=="单边")
{
danbian.inst=str[5].substr(str[5].find(' ')+1);
danbian.entryPrice=str[6].substr(str[6].find(' ')+1);
danbian.step=str[7].substr(str[7].find(' ')+1);
danbian.maxOrder=str[8].substr(str[8].find(' ')+1);
danbian.minOrder=str[9].substr(str[9].find(' ')+1);
}
else if(type=="TB")
{
TB.TBUrl=str[5].substr(str[5].find(' ')+1);
}
cout<<account.password<<endl;
cout<<type<<endl;
cout<<TB.TBUrl<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a;
getAccountInfo();
cin>>a;
return 0;
}