使用C语言读取properties文件V1.0
2015-06-17 10:24
489 查看
本程序使用C语言读取类似以下格式的properties文件。
path = /etc/wgetrc
launch_on_start = true
下一版目标:
(1)使用指针代替二维数据或者二维数据的容量减少至最低要求。
(2)忽略所有空格,忽略空行及注释行
(3)头文件规范写法。
文件1:main.c
文件2:read_properties.h
文件3:read_properties.c
文件4:utils.h
文件5:utils.c
运行结果:
path = /etc/wgetrc
launch_on_start = true
下一版目标:
(1)使用指针代替二维数据或者二维数据的容量减少至最低要求。
(2)忽略所有空格,忽略空行及注释行
(3)头文件规范写法。
文件1:main.c
#include <stdio.h> #include "read_properties.h" int main(void){ char names[100][100], values[100][100]; read_properties("/home/lujinhong/scripts/projects/read_properties_file/test.properties", names, values); return 0; }
文件2:read_properties.h
void read_properties(char *pathname, char names[100][100], char values[100][100]);
文件3:read_properties.c
/******************************************************************** * This file is used to read the names and values from a properties file, * and store them in an array. * * ******************************************************************/ #include "read_properties.h" #include <stdio.h> #include <unistd.h> #include "utils.h" void read_properties(char *pathname, char names[100][100], char values[100][100]){ FILE *file; char line[100]; int i = 0; file = fopen(pathname, "r"); while(fgets(line, 100, file)){ printf("%s", line); //just for test, delete it later. parseline(line, names[i], values[i]); i++; } fclose(file); }
文件4:utils.h
void parseline(char *line, char *name, char *value);
文件5:utils.c
/*********************************************************** * Parse content of the line, and store the name and value. * line example: path=/etc/wgetrc * * *********************************************************/ void parseline(char *line, char *name, char *value){ int length = 0, equal = 1; //equal will record the location of the '=' char *begin; length = strlen(line); for(begin = line; *begin != '=' && equal <= length; begin ++){ equal++; } strncpy(name, line, equal - 1); line+=equal; strncpy(value, line, length - equal); printf("name = %s value = %s\n", name, value); //just for test, delete it later. }
运行结果:
path = /etc/wgetrc name = path value = /etc/wgetrc launch_on_boot = true name = launch_on_boot value = true
相关文章推荐
- Member Function Pointers and the Fastest Possible C++ Delegates
- C语言程序的内存分配方式
- C语言内存分配时间
- C语言声明数组变量时,在什么情况下,可不指定数组大小
- c语言中&lt;stdbool.h&gt;的使用
- C++ 常用函数
- c/c++创建删除文件 文件夹
- 使用了非托管的第三方库的c++动态库在c#中的调用
- Effective C++ 条款52
- 使用VS2010在项目中编写C++头文现出"PCH 警告:标头停止点不能位于宏或#if块中"
- 《深入应用C++11:代码优化与工程级应用》
- c语言结构体用法
- C++ 关于滚动条的滚动问题
- 用C/C++开发基于VLC SDK的视频播放器
- 关于C语言中函数调用和参数传递机制的探讨 (转)
- C/C++中赋值语句的返回值问题
- C++实现DES加密解密
- Effective C++ 条款51
- 【转】[C++]实现委托模型
- 2015年oj刷题:实现复数类中的加运算符重载【C++运算符重载】