社区
C语言
帖子详情
用C语言读取INI文件,怎么实现
cnlaojie
2007-03-27 08:51:22
请问如何在UNIX环境下用标准C来读取INI文件?那位高手可以给出例子,小弟谢谢了!
...全文
1629
10
打赏
收藏
用C语言读取INI文件,怎么实现
请问如何在UNIX环境下用标准C来读取INI文件?那位高手可以给出例子,小弟谢谢了!
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
cnlaojie
2007-03-27
打赏
举报
回复
问题解决了,UNXIN下应该这么写:
#include "stdio.h"
#include "string.h"
static char tmpstr[1024];
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];
char ch;
if ((fp = fopen( filename, "rb")) == 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 );
}
main()
{
char YCQH[20];
strcpy(YCQH,GetInitKey("gameconfig.ini", "Amount", "YCQH"));
printf(YCQH);
}
cnlaojie
2007-03-27
打赏
举报
回复
我测试了
if (( tmp != NULL )&&(flag == 1))
{
if (strstr(tmpLine,key)!=NULL)
{
strcpy ( tmpstr, tmp + 1 );
fclose ( fp );
}
}
这里没有执行丫!
cnlaojie
2007-03-27
打赏
举报
回复
我加了头文件,VC下是可以,我是在UNIX下。
祝云飞
2007-03-27
打赏
举报
回复
执行完后结果是sss
加上头文件string.h试试
cnlaojie
2007-03-27
打赏
举报
回复
有人知道么,在线等
cnlaojie
2007-03-27
打赏
举报
回复
我怎么编译通过以后,执行。没有出来值呢?什么也没有,也没有错误。
#include "stdio.h"
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 "";
}
main()
{
char YCQH[20];
strcpy(YCQH,GetInitKey("gameconfig.INI", "GmaesAmount", "YCQH"));
printf(YCQH);
}
配置文件:
[GmaesAmount]
YCMS=15
YCQH=sss
YCQT=10
YCJW=5
iu_81
2007-03-27
打赏
举报
回复
添加#include "string.h"
iu_81
2007-03-27
打赏
举报
回复
#include "stdio.h"
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 "";
}
main()
{
char YCQH[20];
strcpy(YCQH,GetInitKey("gameconfig.ini", "config", "YCQH"));
}
cnlaojie
2007-03-27
打赏
举报
回复
谢谢jixingzhong(瞌睡虫·星辰),我是下面这样写得,但是出错了,错误信息是:
UX:acomp: ERROR: "laojie_test.c", line 37: identifier redeclared: GetInitKey
请指教!
#include "stdio.h"
main()
{
char YCQH[20];
strcpy(YCQH,GetInitKey("gameconfig.ini", "config", "YCQH"));
}
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 "";
}
jixingzhong
2007-03-27
打赏
举报
回复
读取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
C语言
解析
ini
文件
C语言
ini
文件
解析 linux 下测试程序,使用方法直接make即可
用
C语言
读取
.
ini
文件
在Windows下可以用GetPrivateProfileString或GetPrivateProfileInt方便
读取
.
ini
配置
文件
内容,自己尝试写了一下这个函数。还有一些bug,后面慢慢改。
c语言
读写
ini
文件
使用
c语言
读写
ini
文件
,不适用认识win32系统dll
纯
C语言
读写
ini
配置
文件
使用纯
C语言
写的读写
ini
格式配置
文件
示例,VC6编译,代码和注释详细。封装合理,可快速集成到其它项目。
c语言
实现
ini
文件
读写操作
C语言
实现
的
INI
文件
读写操作是软件开发中常用的一种配置管理技术。
INI
文件
以其简单的结构和易于编辑的特性,成为了存储程序设置和配置信息的首选格式。通过使用
C语言
编写的读写操作函数,开发者可以轻松地在程序中加载和保存配置参数,从而
实现
对软件行为的自定义和灵活调整。 该
INI
文件
读写操作的
实现
主要包括解析
INI
文件
的结构,提取和设置配置项的值,以及将修改后的配置信息写回
文件
。
C语言
提供了丰富的标准库函数,使得处理文本
文件
和字符串操作变得简单高效。通过编写相应的
读取
和写入函数,开发者可以快速地将应用程序的配置信息与
INI
文件
进行同步,确保配置的持久化和一致性。 在实际应用中,
C语言
实现
的
INI
文件
读写操作具有高度的可移植性和稳定性,适用于各种嵌入式系统、桌面应用和服务器程序。此外,由于
C语言
的接近硬件的特性,该读写操作也具有较高的性能,能够在资源受限的环境中可靠地工作。 总之,
C语言
实现
的
INI
文件
读写操作为软件开发提供了一种简单、高效且可靠的配置管理解决方案。无论是对于初学者还是资深开发者,掌握这一技能都能够帮助他们更好地管理和维护软件配置,提高开发效率和软件质量。
C语言
70,022
社区成员
243,263
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章