C++ 读取配置文件

wangzhe1945 2015-01-21 09:12:02
#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;
}
...全文
147 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lafite_MoMo 2015-01-22
  • 打赏
  • 举报
回复
一个C++,一个C# 你想说什么?
wangzhe1945 2015-01-21
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Diagnostics; using System.Threading; namespace CTPtrader { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string m_file = Application.StartupPath + "\\account.txt"; private void Form1_Load(object sender, EventArgs e) { if (File.Exists(m_file)) { string[] lines = File.ReadAllLines(m_file); txtMDUrl.Text = lines[0].Split(' ')[1]; txtTradeUrl.Text = lines[1].Split(' ')[1]; txtAccount.Text = lines[2].Split(' ')[1]; txtPassWord.Text = getPassword(lines[3].Split(' ')[1]); string type = lines[4].Split(' ')[1]; switch (type) { case "套利": cmbType.SelectedIndex = 0; txtLeg1.Text = lines[5].Split(' ')[1]; txtLeg2.Text = lines[6].Split(' ')[1]; txtEndTime.Text = lines[7].Split(' ')[1]; txtEntryPrice.Text = lines[8].Split(' ')[1]; txtStep.Text = lines[9].Split(' ')[1]; txtMaxOrder.Text = lines[10].Split(' ')[1]; txtMinOrder.Text = lines[11].Split(' ')[1]; break; case "单边": cmbType.SelectedIndex = 1; txtInst.Text = lines[5].Split(' ')[1]; txtDanbianEntryPrice.Text = lines[6].Split(' ')[1]; txtDanbianStep.Text = lines[7].Split(' ')[1]; txtDanbianMaxOrder.Text = lines[8].Split(' ')[1]; txtDanbianMinOrder.Text = lines[9].Split(' ')[1]; break; case "TB": cmbType.SelectedIndex = 2; txtTBUrl.Text = lines[5].Split(' ')[1]; break; default: break; } } } private void btnSubmit_Click(object sender, EventArgs e) { string[] content = new string[12]; content[0] = "MDUrl " + txtMDUrl.Text.Trim(); content[1] = "TradeUrl " + txtTradeUrl.Text.Trim(); content[2] = "Account " + txtAccount.Text.Trim(); content[3] = "Password " + putPassword(txtPassWord.Text.Trim()); string type = cmbType.Text.Trim(); content[4] = "Type " + type; switch (type) { case "套利": content[5] = "Leg1 " + txtLeg1.Text.Trim(); content[6] = "Leg2 " + txtLeg2.Text.Trim(); content[7] = "EndTime " + txtEndTime.Text.Trim(); content[8] = "EntryPrice " + txtEntryPrice.Text.Trim(); content[9] = "Step " + txtStep.Text.Trim(); content[10] = "MaxOrder " + txtMaxOrder.Text.Trim(); content[11] = "MinOrder " + txtMinOrder.Text.Trim(); break; case "单边": content[5] = "Inst " + txtInst.Text.Trim(); content[6] = "DanbianEntryPrice " + txtDanbianEntryPrice.Text.Trim(); content[7] = "DanbianStep " + txtDanbianStep.Text.Trim(); content[8] = "DanbianMaxOrder " + txtDanbianMaxOrder.Text.Trim(); content[9] = "DanbianMinOrder " + txtDanbianMinOrder.Text.Trim(); break; case "TB": content[5] = "TBUrl " + txtTBUrl.Text.Trim(); break; default: break; } File.WriteAllLines(m_file, content); Thread thread = new Thread(new ThreadStart(StartCTP)); thread.Start(); } private string putPassword(string pwd) { int cnt = pwd.Length; char[] result = new char[cnt]; for (int i = 0; i < cnt; 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 == '9') { a = '0'; } else if (a == 'Z') { a = 'A'; } else if (a == 'z') { a = 'a'; } result[i] = a; } return new string(result); } private string getPassword(string pwd) { int cnt = pwd.Length; char[] result = new char[cnt]; for (int i = 0; i < cnt; 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'; } result[i] = a; } return new string(result); } private void cmbType_SelectedIndexChanged(object sender, EventArgs e) { tabControl1.SelectTab(cmbType.SelectedIndex); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void StartCTP() { string fileName= "cmd.exe"; ProcessStartInfo ctpExe = new ProcessStartInfo(fileName); //start.Arguments = " log " + _servicesPath; ctpExe.CreateNoWindow = true; ctpExe.RedirectStandardOutput = true; ctpExe.RedirectStandardInput = true; ctpExe.UseShellExecute = false; Process p = Process.Start(ctpExe); //p.StandardInput.WriteLine("svn help "); StreamReader reader = p.StandardOutput; string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); reader.Close(); return ; } } }

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧