Use C language to read the properties file V1.0

Source: Internet
Author: User

This program uses C language to read properties files in a format similar to the following. Path =/etc/wgetrclaunch_on_start = true next version goals: (1) Use a pointer to replace two-dimensional data or reduce the capacity of two-dimensional data to a minimum requirement. (2) Ignore all spaces, ignore empty lines and comment lines (3) header file standard writing. File 1: main. c [cpp] # 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;} file 2: read_properties.h [cpp] void read_properties (char * pathname, char names [100] [100], char values [100] [100]); file 3: read_properties.c [cpp]/************************* **************************************** * *** 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);} file 4: utils. h [cpp] void parseline (char * line, char * name, char * value); File 5: utils. c [cpp] Using * Parse content of the line, and stor E 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 .} running result: [plain] path =/etc/wgetrc name = path value =/etc/wgetrc launch_on_boot = true name = launch_on_boot value = true

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.