使用C语言读取properties文件V1.0
生活随笔
收集整理的這篇文章主要介紹了
使用C语言读取properties文件V1.0
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本程序使用C語言讀取類似以下格式的properties文件。
path = /etc/wgetrc
launch_on_start = true
下一版目標:
(1)使用指針代替二維數(shù)據(jù)或者二維數(shù)據(jù)的容量減少至最低要求。
(2)忽略所有空格,忽略空行及注釋行
(3)頭文件規(guī)范寫法。
文件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/wgetrclaunch_on_boot = true name = launch_on_boot value = true總結
以上是生活随笔為你收集整理的使用C语言读取properties文件V1.0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Subversion进行版本控制
- 下一篇: GoldenGate系列一:快速搭建单向