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;
}
...全文
163 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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 ; } } }
纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。 现将本人精心制作的ini读写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。 主要特点: 1、支持;和#注释符号,支持行尾注释。 2、支持带引号'或"成对匹配的字符串,提取时自动去引号。引号中可带其它引号或;#注释符。 3、支持无section或空section(名称为空)。 4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。 5、支持section、key或=号前后带空格。 6、支持\n、\r、\r\n或\n\r换行格式。 7、不区分section、key大小写,但写入时以新串为准,并保持其大小写。 8、新增数据时,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。 9、支持指定key所在整行删除,即删除该键值,包括注释。 10、可自动跳过格式错误行,修改时仍然保留。 11、修改时保留原注释:包括整行注释、行尾注释(包括前面空格)。 12、修改时保留原空行。以上三点主要是尽量保留原格式。 不足之处: 1、不支持单key多value(逗号分割),只能一次性提取后自行处理。 2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧) 3、不能提取所有section或key名称。 使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux。

33,321

社区成员

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

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