CPP-基础:c++读取ini文件

配置文件格式是
[JP]
K=2EC156673E 2F4240 5595F6

char str[50];
GetPrivateProfileString("JP", "K",NULL, str, sizeof(str),".\\keydog.ini");
得到str后想将其分成三个字符串
str1=2EC156673E
str2=2F4240
str3=5595F6

第一种方法用MFC 
得有这句#include <afx.h>和包含mfc库

CString sz;
GetPrivateProfileString("JP", "K", NULL, sz.GetBuffer(50), 50, "./keydog.ini");
sz.ReleaseBuffer();
int nPos = 0;
CString sz1, sz2, sz3;
sz1 = sz.Tokenize(" ", nPos);
sz2 = sz.Tokenize(" ", nPos);
sz3 = sz.Tokenize(" ", nPos);

第二种用标准c++
得#include <string> 和using std::string;

string str(50, 0);
GetPrivateProfileString("JP", "K", NULL, (char*)str.c_str(), 50, "./keydog.ini");
string sz1, sz2, sz3;
int nBegin = 0, nEnd = str.find(' ', nBegin);
sz1 = str.substr(nBegin, nEnd - nBegin);
nBegin = nEnd + 1, nEnd = str.find(' ', nBegin);
sz2 = str.substr(nBegin, nEnd - nBegin);
nBegin = nEnd + 1, nEnd = str.find(' ', nBegin);
sz3 = str.substr(nBegin, nEnd - nBegin);

第三种纯c吧...
得#include <string.h>

char str[50] = {0};
GetPrivateProfileString("JP", "K", NULL, str, sizeof(str),".\\keydog.ini");
char sz1[50] = {0}, sz2[50] = {0}, sz3[50] = {0};
sscanf( str, "%[^' ']", sz1);
sscanf( str + strlen(sz1) + 1, "%[^' ']", sz2);
sscanf( str + + strlen(sz1) + strlen(sz2) + 2, "%[^' ']", sz3);

再给个傻瓜版c代码吧, 因为str1等没有另外给空间, 看你怎么用了
#include <stdio.h>
#include <windows.h>

int main()
{
char str[50], *str1, *str2, *str3;
GetPrivateProfileString("JP", "K",NULL, str, sizeof(str),".\\keydog.ini");
str2 = str1 = str;
while( ' ' != *++str2);
*str2++ = 0;
for( str3 = str2; ' ' != *++str3; );
*str3++ = 0;

return 0;
}

转载于:https://www.cnblogs.com/CPYER/p/3466928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值