50分问一个读文件的问题,问题清晰,要求明确,在线等待

shuchao 2007-04-12 11:10:06
将一个配置文件的相应项读进一个结构体中
结构体如下
struct config_file_common
{
char work_dir[20];
char ip[20];
unsigned short port;
unsigned int timeout;
unsigned int poll_time;
unsigned int serv_num;
//char **prog_name;
char log_name[20];
};
...全文
329 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjon 2007-04-13
  • 打赏
  • 举报
回复
用一个函数来完成,有点复杂.
godwater8 2007-04-13
  • 打赏
  • 举报
回复
这个貌似不是技术问题呀 悬赏的吧 有¥¥么?
taodm 2007-04-13
  • 打赏
  • 举报
回复
学正则吧,正则天生就是解决
<xxxx>
yyyy_prog_name = "对xxxx服务进行yyyy方式查询的程序的名称和参数"
</xxxx>
这种东西的。
roger_77 2007-04-13
  • 打赏
  • 举报
回复
<xxxx>
yyyy_prog_name = "对xxxx服务进行yyyy方式查询的程序的名称和参数"
</xxxx>

就这一项把问题搞复杂了
shuchao 2007-04-13
  • 打赏
  • 举报
回复
谢谢各位老师,我的配置文件就是一个简单的文本文件 TXT的,
而且现在
yyyy_prog_name = "对xxxx服务进行yyyy方式查询的程序的名称和参数"
这个东西暂时先忽略吧,有人能给出完整函数代码么
mrdone 2007-04-13
  • 打赏
  • 举报
回复
自己写代码吧.
例如:
[aaa]
bbb=c
用GetPrivateProfileString("aaa","bbb",NULL,c.length(),MaxLength);
或者:GetPrivateProfileInt("aaa","bbb",NULL,c);
Wolf0403 2007-04-13
  • 打赏
  • 举报
回复
虫子闪边。。lz 的文件格式明显不是 ini 而是类似 Apache 的格式。。
jixingzhong 2007-04-13
  • 打赏
  • 举报
回复
这个函数读取的是一个 键值,
如果需要使用结构体,
方法1: 自定义函数,对每个结构体成员调用上面的函数读取ini文件;
方法2: 修改函数内容,读取多个配置项即可~
jixingzhong 2007-04-13
  • 打赏
  • 举报
回复
读取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
shuchao 2007-04-12
  • 打赏
  • 举报
回复
要求,可以完成这项工作的函数声明为int load_config_file(const FILE *fp, struct config_file_comm *con_file);
能给出该函数的完整代码
shuchao 2007-04-12
  • 打赏
  • 举报
回复
配置文件如下(每项一行):
workdir = xxx
ip = xxx
port = xxx
timeout = xxx
poll_time = xxx
serv_num = xxx

<xxxx>
yyyy_prog_name = "对xxxx服务进行yyyy方式查询的程序的名称和参数"
</xxxx>

log_file_name = xxx

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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