Click here to Skip to main content
15,867,771 members
Articles / Desktop Programming / MFC

INI Manipulation using C

Rate me:
Please Sign up or sign in to vote.
1.92/5 (11 votes)
22 Aug 2006CPOL 43.5K   1.2K   13   10
Using C to read and write INI file
Sample Image - Screen.jpg

Introduction

This project uses C and linklist to implement and manage INI files. This project is a sample of linklist implementation.

Sample Usage

C++
ini_start("sample.ini");
//save_as("test.ini");
buffer = get_value("Main","PLC");
printf("value : %s\n",buffer);
printf("last error is %s\n",get_last_error());
//print_content();
ini_end();

How To Implement

This project uses linklist to make a sample tree for collecting INI file data. INI file in this project must separate using "=". This is a sample function to manage INI file.

C++
/*****************************************************************/
/* main ini manage function                                             */
/************************************************************************/
/**
 *    function: ini_start                                                        
 *    parameter:                                                        
 *        @filename                                                            
 *    return:    true if success
 *    purpose: for start ini manipulate file
 */
bool ini_start(const char* filename);

/**
 *    function:    load                                                    
 *    parameter:                                                        
 *        @filename                                                            
 *    return:        true if success
 *    purpose:    for load ini file to content
 */

bool load(const char *filename);

/**
 *    function:    _save                                                    
 *    parameter:                                                        
 *        @filename                                                            
 *    return:        true if success
 *    purpose:    save content to ini file(save as)
 */
bool save();    // save to load filebool 
bool save_as(const char *filename);

/**
 *    function:    get_value                                                    
 *    parameter:                                                        
 *        @                                                            
 *    return:        value
 *    purpose:    ?                                                    
 */
char *get_value (const char *sec,const char *key);
char *_get_value(const char *sec,const char *key,   // return data and comment
                 char *comment);
/**
 *    function:    set_value
 *    parameter:                                                        
 *        @                                                            
 *    return:        true if success
 *    purpose:            
 */
bool set_value  (const char *sec,const char *key,        // will auto replace
                 const char *value);    
bool _set_value (const char *sec,const char *key,    // select replace or not replace
                 const char *value,const char *comment,REPLACE_FLAG flag);

int  remove_sel (const char *sec,char *key);
int  remove_all (const char * sec);                // remove all record in section

// add/remove section
void add_section(const char *sec,const char *comment);    // add section
int  remove_section(char *sec);  	// remove section (remove all record 
				// in section if not empty)

void clear();                                	// clear all content

// size of section
int  content_size();
int  section_size(char *sec);

// for console display use stdio.h stdout
void print_content();                        	// print all content
void print_section();                        	// print all only section
void print_record(char *sec,char *key);   	// print selection record
void print_allrecord(char *sec);            	// print all record in section

/**
 *    function: ini_end                                                    
 *    parameter:                                                        
 *        none                                                                    
 *    return:    void                                                        
 *    purpose: for end ini manipulate file
 */
void ini_end();
void _ini_end(REPLACE_FLAG flag);

/**
 *    function: get_last_error
 *    parameter:                                                        
 *        none                                                            
 *    return:    type of error   
 *    purpose:    for get error      
 */
char *get_last_error();

History

  • 22nd August, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Thailand Thailand
C++,Window,Linux Programming
Measurement and Control System.

Comments and Discussions

 
Questionversion for Linux ? Pin
dereach26-Jan-09 0:18
dereach26-Jan-09 0:18 
AnswerRe: version for Linux ? Pin
TooBaya16-Apr-09 17:48
TooBaya16-Apr-09 17:48 
GeneralBest and Simple INI Manipulation I ever Seen Pin
lv4415-May-08 7:16
lv4415-May-08 7:16 
Generalcfind a bug Pin
yangqi.cn11-Sep-06 23:10
yangqi.cn11-Sep-06 23:10 
Generalimplementation... Pin
brainunit22-Aug-06 21:03
brainunit22-Aug-06 21:03 
GeneralThank for comment Pin
TooBaya22-Aug-06 20:49
TooBaya22-Aug-06 20:49 
Thank for message and comment.
But for this project,I want to show ini manipulate using linklist and c programming.
It can run on linux and unix and windows.

I love my style.

GeneralRe: Thank for comment Pin
DaTxomin22-Aug-06 20:56
DaTxomin22-Aug-06 20:56 
GeneralYou can use Win32 SDK functions ... Pin
Robert W.22-Aug-06 20:39
Robert W.22-Aug-06 20:39 
GeneralRe: You can use Win32 SDK functions ... Pin
yama no mura9-Oct-07 21:55
yama no mura9-Oct-07 21:55 
NewsINI files are dead Pin
Toby Opferman22-Aug-06 20:21
Toby Opferman22-Aug-06 20:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.