一个有关字符串解析的问题

flymoon99 2007-03-15 02:45:53
有这样一段文本

[agent]
value1=111
value2=222
[back]
value1=12
value2=23

要求写一个通用的函数,函数中有2个参数a和b,当输入a为agent,b为value1时输出111;输入a为back,b为value2时输出23.

小弟不才,希望有人能提供个sample
...全文
174 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
flymoon99 2007-03-15
  • 打赏
  • 举报
回复
谢谢 jixingzhong(瞌睡虫·星辰) 的提示,我懂了!
anguofeng 2007-03-15
  • 打赏
  • 举报
回复
用楼上就可以了
jixingzhong 2007-03-15
  • 打赏
  • 举报
回复
根据这个,
做少量修改即可 ~
jixingzhong 2007-03-15
  • 打赏
  • 举报
回复
读取INI文件的函数(C语言)


/*
* File: inifile.h
* Read INI File
*/
#ifndef _INIFILE_H_
#define _INIFILE_H_

#include
#include

/*
* char* GetInitKey(FileName, Section, Key)
* Return Key=>Value
* Ex:
*
* + [config]
* + dbhost=localhost
*
* strcpy(dbhost,GetInitKey("config.ini", "config", "dbhost"));
*/
char * GetInitKey(char *filename, char *title,char *key)
{
FILE * fp;
char tmpLine[1024];
int rtnval;
int i = 0;
int flag = 0;
char * tmp;
static char tmpstr[1024];

if ((fp = fopen( filename, "r")) == NULL )
{
return "have no such file";
}
while (!feof(fp))
{
rtnval = fgetc( fp );
if ( rtnval == EOF )
{
break;
}
else
{
tmpLine[i++] = rtnval;
}
if ( rtnval == '\n')
{
tmpLine[--i]=0;
i = 0;
tmp = strchr(tmpLine, '=');

if (( tmp != NULL )&&(flag == 1))
{
if (strstr(tmpLine,key)!=NULL)
{
strcpy ( tmpstr, tmp + 1 );
fclose ( fp );
return tmpstr;
}
}
else
{
strcpy(tmpstr,"[");
strcat(tmpstr,title);
strcat(tmpstr,"]");
if (strcmp(tmpstr,tmpLine)==0)
{
flag = 1;
}
}

}
}
fclose ( fp );
return "";
}
#endif //_INIFILE_H_

使用方法:

char dbhost[20];
strcpy(dbhost,GetInitKey("config.ini", "config", "dbhost"));

ini文件内容

[config]
dbhost=localhost
iamcaicainiao 2007-03-15
  • 打赏
  • 举报
回复
这个布什很简单么?


自己写下。
麦田之兔 2007-03-15
  • 打赏
  • 举报
回复
自己写也可以不过直接用编译器带的函数就可以了
todototry 2007-03-15
  • 打赏
  • 举报
回复
可自个的分析
todototry 2007-03-15
  • 打赏
  • 举报
回复
getprofilestring
todototry 2007-03-15
  • 打赏
  • 举报
回复
这种格式用ini的api

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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