c++ 读取配置文件ini
exe吧
全部回复
仅看楼主
level 4
黄山Felix 楼主

404错误页面
内容来自:百度
2013年11月01日 09点11分 1
level 9
404
2013年11月01日 15点11分 2
level 4
黄山Felix 楼主
c++ 读取配置文件ini
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string>
#include <map>
using namespace std;
#define CONFIG "readini.ini"
#define EQU "="
#define SPACE " "//0x20
#define CR "\r"//0x0d
#define CRLF "\r\n"//0x0a
map<std::string, std::string> read_conf(char *filename)
{
FILE *fp;
char line[512];
char *note= "#";
char *s1, *s2;
map<std::string, std::string> m_date;
printf("---------------filename:%s---------------\n", filename);
if ((fp=fopen(filename, "rb"))==NULL)
{
printf("read_conf can not open conf file %d\n", filename);
return m_date;
}
while(fgets(line, 512, fp))
{
//判断有没有注释符
if (!strlen(line) || !strncmp(line, note, 1) || !strcmp(line, CR) || !strcmp(line, CRLF))
{
continue;
}
s1= strtok(line, EQU);
s2= strtok(NULL, CRLF);
if (!s2)
{
s2= strtok(NULL, CR);//判断每行有没有回车符
}
if (!s1 || !s2)
{
continue;
}
printf("#fkey: %s\t#fval: %s\n", s1, s2);
m_date[s1]= s2;
}
return m_date;
}
int main()
{
map<std::string, std::string> m_conf;
m_conf= read_conf( CONFIG );
printf("---------------show map infos---------------\n");
map<std::string, std::string>::iterator it;
for(it=m_conf.begin(); it!= m_conf.end();it++)
{
printf("#rkey: %s\t#rval: %s\n", it->first.c_str(), it->second.c_str());
}
int a;
cin>>a;
return 0;
}
2.
/////MFC版本
写文件
//LPTSTR lpPath = new char[MAX_PATH];
//strcpy(lpPath, "D:\\IniFileName.ini");
// WritePrivateProfileString("LiMing", "Sex", "Man", lpPath);
// WritePrivateProfileString("LiMing", "Age", "20", lpPath);
//WritePrivateProfileString("Fangfang", "Sex", "Woman", lpPath);
//WritePrivateProfileString("Fangfang", "Age", "21", lpPath);
// delete [] lpPath;
读文件
LPTSTR lpPath = new char[MAX_PATH];
LPTSTR LiMingSex = new char[6];
int LiMingAge;
LPTSTR FangfangSex = new char[6];
int FangfangAge;
strcpy(lpPath, "D:\\IniFileName.ini");
GetPrivateProfileString("LiMing", "Sex", "", LiMingSex, 6, lpPath);
LiMingAge = GetPrivateProfileInt("LiMing", "Age", 0, lpPath);
GetPrivateProfileString("Fangfang", "Sex", "", FangfangSex, 6, lpPath);
FangfangAge = GetPrivateProfileInt("Fangfang", "Age", 0, lpPath);
delete [] lpPath;
I
2013年11月03日 12点11分 3
level 1
详情到易百纳社区
2020年07月21日 11点07分 4
1