C language Implementation Read Write INI file implementation (GO)

Source: Internet
Author: User
Tags sprintf

#include <stdio.h>#include<string.h>/** Function Name: getinikeystring * Entry parameter: Title * Identification of a set of data in a profile * key * The identity of the value to be read in this set of data * filename * The file path to read * Return value: Find the value to check Returns the correct result * otherwise returns null*/  Char*getinikeystring (Char*title,Char*key,Char*filename) {FILE*FP; intFlag =0; Charstitle[ +], *wTmp; Static Charsline[1024x768]; sprintf (Stitle,"[%s]", title); if(NULL = = (fp = fopen (filename,"R")) {perror ("fopen"); returnNULL; }         while(NULL! = fgets (sline,1024x768, FP)) {          //This is the comment line        if(0= = STRNCMP ("//", Sline,2))Continue; if('#'= = sline[0])Continue; WTMP= STRCHR (Sline,'='); if((NULL! = wTmp) && (1==flag)) {              if(0= = strncmp (key, Sline, Wtmp-sline)) {//length According to the file read the subjectSline[strlen (sline)-1] =' /';                  Fclose (FP); returnWTMP +1; }          } Else {              if(0= = strncmp (Stitle, Sline, strlen (sline)-1)) {//length According to the file read the subjectFlag =1;//Find title Position}}} fclose (FP); returnNULL; }    /** Function Name: Getinikeyint * Entry parameter: Title * Identification of a set of data in a profile * key * The identity of the value to be read in this set of data * filename * The file path to read * Return value: Find the value to be checked returns the positive True result * Otherwise null*/  intGetinikeyint (Char*title,Char*key,Char*filename) {      returnatoi (getinikeystring (title, key, filename)); }    /** Function Name: putinikeystring * Entry parameter: Title * Identification of a set of data in a profile * key *                      The identity of the value to be read in this set of data * val * Changed value * filename * The file path to be read * return value: 0 returned successfully * Otherwise-1*/  intPutinikeystring (Char*title,Char*key,Char*val,Char*filename) {FILE*FPR, *FPW; intFlag =0; Charsline[1024x768], stitle[ +], *wTmp; sprintf (Stitle,"[%s]", title); if(NULL = = (FPR = fopen (filename,"R")) ) Prn_errmsg_return ("fopen");//read the original filesprintf (Sline,"%s.tmp", filename); if(NULL = = (Fpw = fopen (sline,"W")) ) Prn_errmsg_return ("fopen");//Write temporary files       while(NULL! = fgets (sline,1024x768, FPR)) {          if(2! = Flag) {//If you find the row that you want to modify, the internal action is not performedWTMP = STRCHR (sline,'='); if((NULL! = wTmp) && (1==flag)) {                  if(0= = strncmp (key, Sline, Wtmp-sline)) {//length According to the file read the subjectFlag =2;//change values for easy writing to filessprintf (WTMP +1,"%s\n", Val); }              } Else {                  if(0= = strncmp (Stitle, Sline, strlen (sline)-1)) {//length According to the file read the subjectFlag =1;//Find title Position}}} fputs (Sline, FPW); //Write temporary files} fclose (FPR);        Fclose (FPW); sprintf (Sline,"%s.tmp", filename); returnRename (sline, filename);//Update temporary files to the original file}    /** Function Name: putinikeystring * Entry parameter: Title * Identification of a set of data in a profile * key *                      The identity of the value to be read in this set of data * val * Changed value * filename * The file path to be read * return value: 0 returned successfully * Otherwise-1*/  intPutinikeyint (Char*title,Char*key,intValChar*filename) {      Charsval[ +]; sprintf (Sval,"%d", Val); returnputinikeystring (title, key, Sval, filename); }        intMainintargcChar*argv[]) {printf ("%s\n", Getinikeystring ("DOG","name","Config.ini")); printf ("%d\n", Getinikeyint ("DOG"," Age","Config.ini")); Putinikeystring ("CAT","name","CCC","Config.ini"); Putinikeyint ("DOG"," Age", About,"Config.ini"); return 0; }  
This is a config.ini file I created myself.

#[cat]
[CAT]
Name=dsfdsf
Age=34
Name=aaa
Name1=ooo
[DOG]
[DOG]
name=bbb
Name1=xxx
Age=25

When you run the program. The result: BBB
25 last Config.ini file changed to: #[cat]
[CAT]
Name=dsfdsf
Age=34
Name=ccc
Name1=ooo
[DOG]
[DOG]
name=bbb
Name1=xxx
age=56==========================================================================================http:// blog.csdn.net/niha1993825jian/article/details/41086403

C language Implementation Read Write INI file implementation (GO)

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.