---
title: "C读写ini文件"
author: "xpp02"
date: "2013-02-02T16:13:00Z"
tags: []
slug: "c-ini"
draft: false
lastmod: "2013-02-02T21:33:57Z"
id: 1784217
url: "https://xpp02.iteye.com/blog/1784217"
---

/*  
read/write ini file with c function  
@file testini.c  
chinayaosir  
blog: http://blog.csdn.net/chinayaosir  
connect.ini  
\[database]  
此程序有些BUG  
当ini文件不存在时，第一次建立connect.ini文件时，  
在\[database]前面会多一个空格.  
\*/  
#include "stdio.h"  
#include "inifile.h"  
#define BUF\_SIZE 256

int main()  
{  
const char * file = "connect.ini";  
char severname\[BUF\_SIZE] = {0};  
int port=0;

printf( "1. write/read ini file testting .../n" );

printf( "2. write data into ini file.../n" );

if (! write\_profile\_string("database", "sever" , "192.168.1.2",file))

{ printf( "2.1 server write %s file fail/n",file ); }  
else  
{  
printf( "2.1 server write %s file ok/n",file );  
}  
if (! write\_profile\_string("database", "port" , "5000",file))  
{ printf( "2.2 port write %s file fail/n",file ); }  
else  
{  
printf( "2.2 port write %s file ok/n",file );  
}

printf( "3. read data from ini file.../n" );

if ( ! read\_profile\_string("database", "sever", severname,BUF\_SIZE,file))  
{ printf( "3.1 sever read %s file fail/n",file ); }  
else  
{ printf( "3.1 sever = %s/n" ,severname);}

if ( ! read\_profile\_int("database", "port", port,file))  
{ printf( "3.2 port read %s file fail/n",file ); }  
else  
{ port=read\_profile\_int("database", "port", port,file);  
printf( "3.2 port = %d/n" ,port);  
}

return 0 ;

}

/*  
run value:  
1\. write/read ini file testting ...  
2\. write data into ini file...  
2.1 server write connect.ini file ok  
2.2 port write connect.ini file ok  
3\. read data from ini file...  
3.1 sever = 192.168.1.2  
3.2 port = 5000  
\*/

/////////////////////////////////////////////////////////////////////////  
/* *  
\*@file inifile.h  
\*@cpright (C)2007 GEC  
\*@auther dengyangjun  
\*@email dyj057@gmail.com  
\*@version 0.1  
\*@create 2007-1-14  
\*@modify 2007-1-14  
\*@brief declare ini file operation  
\*@note  
\*@history  
\*/

#ifndef INI\_FILE\_H_  
#define INI\_FILE\_H_

#ifdef \_\_cplusplus  
extern " C "  
{  
#endif

int read\_profile\_string(const char * section,const char * key,char * value,int size,const char * file);  
int read\_profile\_int( const char * section,const char * key, int default\_value,const char * file);  
int write\_profile\_string(const char * section,const char * key,const char * value, const char * file);

#ifdef \_\_cplusplus  
}; // end of extern "C" {  
#endif

#endif // end of INI\_FILE\_H_

//////////////////////////////////////////////////////////////////////////////  
/* *  
\*@file inifile.c  
\*@cpright (C)2007 GEC  
\*@auther dengyangjun  
\*@email dyj057@gmail.com  
\*@version 0.1  
\*@create 2007-1-14  
\*@modify 2007-1-14  
\*@brief implement ini file operation  
\*@note  
\*@history  
\*/  
#include &lt;stdio.h&gt;  
#include &lt;stdlib.h&gt;  
#include &lt;assert.h&gt;  
#include &lt;string.h&gt;  
#include &lt;ctype.h&gt;

#include "inifile.h"

#ifdef \_\_cplusplus  
extern " C "  
{  
#endif

#define MAX\_FILE\_SIZE 8096

#define LEFT\_BRACE '['  
#define RIGHT\_BRACE ']'

static int load\_ini\_file(const char * file, char * buf, int * file\_size)  
{  
int i = 0 ;  
FILE * in = NULL;  
\*file\_size = 0 ;  
assert(file != NULL);  
assert(buf != NULL);

in = fopen(file, "r" );  
if ( NULL == in ) {  
return 0 ;  
}

// load initialization file  
while ((buf\[i] = fgetc(in)) != EOF) {  
i ++ ;  
assert( i &lt; MAX\_FILE\_SIZE); // file too big  
}

buf\[i] = '/0' ;  
\*file\_size = i;

fclose(in);  
return 1 ;  
}

/*  
\*&lt;summary&gt;&lt;/summary&gt;  
\* &lt;returns&gt;Result of the addtion(int)&lt;/returns&gt;  
\* &lt;param name="x"&gt;&lt;/param&gt;  
\* &lt;param name="y"&gt;&lt;/param&gt;  
\*/

static int isnewline( char c)  
{  
return ( '/n' == c || '/r' == c ) ? 1 : 0 ;  
}  
static int isend( char c)  
{  
return '/0' == c ? 1 : 0 ;  
}  
static int isleftbarce( char c)  
{  
return LEFT\_BRACE == c ? 1 : 0 ;  
}  
static int isrightbrace( char c )  
{  
return RIGHT\_BRACE == c ? 1 : 0 ;  
}

static int parse\_file(const char * section,  
const char * key,  
const char * buf,  
int * sec\_s,  
int * sec\_e,  
int * key\_s,  
int * key\_e,  
int * value\_s,  
int * value\_e)  
{  
const char * p = buf;  
int i = 0 ;

assert(buf != NULL);  
assert(section != NULL && strlen(section));  
assert(key != NULL && strlen(key));

\* sec\_e = * sec\_s = * key\_e = * key\_s = * value\_s = * value\_e = - 1 ;

while ( ! isend(p\[i]) )  
{  
// find the section  
if ( ( 0 == i || isnewline(p\[i - 1 ]) ) && isleftbarce(p\[i]) )  
{  
int section\_start = i + 1 ;

// find the ']'  
do  
{  
i ++ ;  
} while ( ! isrightbrace(p\[i]) && ! isend(p\[i]));

if ( 0 == strncmp(p + section\_start,section, i - section\_start))  
{  
int newline\_start = 0 ;

i ++ ;

// Skip over space char after ']'  
while (isspace(p\[i]))  
{  
i ++ ;  
}

// find the section  
\* sec\_s = section\_start;  
\* sec\_e = i;

while ( ! (isnewline(p\[i - 1 ]) && isleftbarce(p\[i])) && ! isend(p\[i]) )  
{  
int j = 0 ;  
// get a new line  
newline\_start = i;

while ( ! isnewline(p\[i]) && ! isend(p\[i]) )  
{  
i ++ ;  
}  
// now i is equal to end of the line

j = newline\_start;

if ( ';' != p\[j]) // skip over comment  
{  
while (j &lt; i && p\[j] != '=' )  
{  
j ++ ;  
if ( '=' == p\[j])  
{  
if (strncmp(key,p + newline\_start,j - newline\_start) == 0 )  
{  
// find the key ok  
\* key\_s = newline\_start;  
\* key\_e = j - 1 ;

\* value\_s = j + 1 ;  
\* value\_e = i;

return 1 ;  
}  
}  
}  
}

i ++ ;  
}  
}  
}  
else  
{  
i ++ ;  
}  
}  
return 0 ;  
}

/* *  
\* @brief read\_profile\_string &lt;d&gt;retrieves a string from the specified section in an initialization file.  
\* @param section &lt;t&gt;const char * &lt;in&gt; &lt;d&gt;name of the section containing the key name.  
\* @param key &lt;t&gt;const char * &lt;in&gt;&lt;d&gt;the name of the key whose associated string is to be retrieved.  
\* @param value &lt;t&gt;char * &lt;out&gt;&lt;d&gt;pointer to the buffer that receives the retrieved string.  
\* @return &lt;t&gt;int &lt;n&gt;1 : read success; 0 : read fail.  
\*/  
int read\_profile\_string(  
const char * section,  
const char * key,  
char * value,  
int size,  
const char * file)  
{  
char buf\[MAX\_FILE\_SIZE] = { 0 };  
int file\_size;  
int sec\_s,sec\_e,key\_s,key\_e, value\_s, value\_e;

// check parameters  
assert(section != NULL && strlen(section));  
assert(key != NULL && strlen(key));  
assert(value != NULL);  
assert(size &gt; 0 );  
assert(file != NULL && strlen(key));

if ( ! load\_ini\_file(file,buf, & file\_size))  
return 0 ;

if ( ! parse\_file(section,key,buf, & sec\_s, & sec\_e, & key\_s, & key\_e, & value\_s, & value\_e))  
{  
return 0 ; // not find the key  
}  
else  
{  
int cpcount = value\_e - value\_s;

if ( size - 1 &lt; cpcount)  
{  
cpcount = size - 1 ;  
}

memset(value, 0 , size);  
memcpy(value,buf + value\_s, cpcount );  
value\[cpcount] = '/0' ;

return 1 ;  
}  
}

int read\_profile\_int(const char \*section,  
const char \*key,  
int default\_value,  
const char \*file)  
{  
char value\[ 32 ] = { 0 };  
if ( ! read\_profile\_string(section,key,value, sizeof (value),file))  
{  
return default\_value;  
}  
else  
{  
return atoi(value);  
}  
}

int write\_profile\_string(const char * section,  
const char * key,  
const char * value,  
const char * file)  
{  
char buf\[MAX\_FILE\_SIZE] = { 0 };  
char w\_buf\[MAX\_FILE\_SIZE] = { 0 };  
int sec\_s,sec\_e,key\_s,key\_e, value\_s, value\_e;  
int value\_len = ( int )strlen(value);  
int file\_size;  
FILE * out ;

// check parameters  
assert(section != NULL && strlen(section));  
assert(key != NULL && strlen(key));  
assert(value != NULL);  
assert(file != NULL && strlen(key));

if (!load\_ini\_file(file,buf, & file\_size))  
{  
sec\_s = - 1 ;  
}  
else  
{  
parse\_file(section,key,buf, & sec\_s, & sec\_e, & key\_s, & key\_e, & value\_s, & value\_e);  
}

if ( - 1 == sec\_s)  
{

if ( 0 == file\_size)  
{  
sprintf(w\_buf + file\_size, " \[%s]/n%s=%s/n" ,section,key,value);  
}  
else  
{  
// not find the section, then add the new section at end of the file  
memcpy(w\_buf,buf,file\_size);  
sprintf(w\_buf + file\_size, "/n\[%s]/n%s=%s/n" ,section,key,value);  
}

}  
else if ( - 1 == key\_s)  
{  
// not find the key, then add the new key & value at end of the section  
memcpy(w\_buf,buf,sec\_e);  
sprintf(w\_buf + sec\_e, "%s=%s/n" ,key,value);  
sprintf(w\_buf + sec\_e + strlen(key) + strlen(value) + 2 ,buf + sec\_e, file\_size - sec\_e);  
}  
else  
{  
// update value with new value  
memcpy(w\_buf,buf,value\_s);  
memcpy(w\_buf + value\_s,value, value\_len);  
memcpy(w\_buf + value\_s + value\_len, buf + value\_e, file\_size - value\_e);  
}

out = fopen(file, "w" );  
if (NULL == out )  
{  
return 0 ;  
}

if ( - 1 == fputs(w\_buf, out ) )  
{  
fclose( out );  
return 0 ;  
}

fclose( out );

return 1 ;  
}

#ifdef \_\_cplusplus  
}; // end of extern "C" {  
#endif