C语言:使用配置文件
来源:互联网 发布:吊装计算软件 编辑:程序博客网 时间:2024/06/01 08:13
#include <stdio.h>#include <string.h>#define APP_MODE_CHAR 1#define APP_MODE_INT 2#define APP_TURE 0#define APP_FALSE 1 static char g_filename[20]="config.txt";//文件名int APP_GetInfo(int mode, char *name,char *infochar, int *infoint){FILE *fd = NULL;char buffer[100];//每行最多100字节int num = 1;//打开文件fd = fopen(g_filename, "r");if ( NULL == fd){printf("fopen error!\n");return APP_FALSE;}//读取文件信息while (1){if(fgets( buffer, 100, fd)== NULL){break;}num = strlen(buffer);if ( buffer[0] != '#' && num > 2){if(strncmp(buffer,name,strlen(name)) == 0){switch(mode){case APP_MODE_CHAR : sscanf(buffer,"%*[^=]=%s",infochar); printf("%s : %s\n",name,infochar); break;case APP_MODE_INT : sscanf(buffer,"%*[^=]= %d",infoint); printf("%s : %d\n",name,*infoint); break;}}}}if ( num <= 0 ){return APP_FALSE;}return APP_TURE;}int main(int argc, char *argv){int ret;char A;int B;int temp;char temp1[3];ret = APP_GetInfo(APP_MODE_CHAR,"MY_NAME",temp1,&B);if ( APP_TURE != ret){printf("cannot find name \n");return 1;}ret = APP_GetInfo(APP_MODE_INT,"MY_AGE",&A,&temp);if ( APP_TURE != ret){printf("cannot find age \n");return 1;}return 0;}
文件config.txt:
#年龄
MY_AGE=23
#姓名
MY_NAME=张三
0 0
- C语言:使用配置文件
- C语言读写配置文件
- c语言读写配置文件
- C语言读取配置文件
- C语言读写配置文件
- c语言读写配置文件
- C语言--配置文件解析
- emacs配置文件 c语言
- c语言读写配置文件
- C语言读取配置文件
- C语言读取配置文件
- C语言读取配置文件
- 配置文件解析函数(C语言)
- 配置文件解析函数(C语言)
- C语言读写配置文件--转载
- C语言读取配置文件例子
- C语言读取配置文件代码
- 用C语言读取配置文件
- python Hadoop Streaming程序测试
- Python写的简单的端口监听,显示端口上收到的数据,TCP的
- C#正则表达式Regex类使用
- 求数组的子数组之和的最小值
- 剑指XX游戏(四) - 轻松搞定面试中的“虚”
- C语言:使用配置文件
- Java 强制类型转换(类转换注意事项)
- 用C++进行设计模式的解析和实现
- java中的文件输入输出流,实现复制粘贴
- 支持向量机学习笔记:数学过程及经典Tutorial
- Android DPAD not enabled in AVD
- SQLite实现在线电子词典
- HDU 1231(最大连续子序列)
- 安卓模拟器Snapshot