怎样用C编写读取INI的配置文件?
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!
; //读字符串 long GetProfileLong(char *pStrSegment,char *pStrKey,long lDefault=0L);//读32位整数 WORD GetProfileWord(char *pStrSegment,char *pStrKey,WORD wDefault);//读16位无符号整数 int GetProfileInt(char *pStrSegment,char *pStrKey, int nDefault);//读16位有符号整数 float GetProfileFloat(char *pStrSegment,char *pStrKey,float fDefault);//读单精度浮点数 double GetProfileDouble(char *pStrSegment,char *pStrKey,double dbDefault);//读双精度浮点数 //写键值 BOOL WriteProfileString(char *pStrSegment,char *pStrKey,char *pStrDefault);//写字符串 BOOL WriteProfileLong(char *pStrSegment,char *pStrKey,long lDefault);//写32位有符号整数 BOOL WriteProfileWord(char *pStrSegment,char *pStrKey,WORD wDefault);//写16位无符号整数 BOOL WriteProfileInt(char *pStrSegment,char *pStrKey, int nDefault);//写16位于符号整数 BOOL WriteProfileFloat(char *pStrSegment,char *pStrKey,float fDefault);//写单精度浮点数 BOOL WriteProfileDouble(char *pStrSegment,char *pStrKey,double dbDefault);//写双精度浮点数3:使用举例#include “Symbol.h”#include “Register.h”BOOL MySample_27(){CRegister* pRegister = new CRegister("Config.Ini"
; //加载文件if (pRegister == NULL || NULL == pRegister->GetFile()) //判断是否加载成功{ DELETE(pRegister); return(FALSE);}//读一整数int nHello = pRegister->GetProfileInt(” Boot”, “Hello”, 37);//读一无符号整数int nSign = pRegister->GetProfileInt(” Boot”, “Sign”, 37u);//读一长整数long lgGood = pRegister->GetProfileLong(” Boot”, “Good”, 37L);//读单精度浮点数float fData = pRegister->GetProfileFloat(” Boot”, ”LiPing”, 0.5f);//读双精度浮点数double dbData = pRegister->GetProfileDouble(” Boot”, ”Line4”, 3.7);//读布尔变量BOOL bBoolV = pRegister->IsProfileBoolen(pRegister->GetProfileString(“Boot”,”BootV”,”True”));//读字符串char strPrintName;_fstrcpy(strPrintName, pRegister->GetProfileString(” Boot”, “Print”, “LPT1”)); //写一整数pRegister->WriteProfileInt(” Boot”, “Hello”, nHello);//写一无符号整数pRegister->WriteProfileInt(” Boot”, “Sign”, nSign);//写一长整数pRegister->WriteProfileLong(” Boot”, “Good”, lgGood);//写单精度浮点数pRegister->WriteProfileFloat(” Boot”, ”LiPing”, fData);//写双精度浮点数pRegister->WriteProfileDouble(” Boot”, ”Line4”, dbData);//写布尔变量pRegister->WriteProfileString(“Boot”,”BootV”,((bBoolV)?”True”:”False”));//写字符串pRegister->WriteProfileString(” Boot”, “Print”, strPrintName); delete pRegister;}
a=1
b=2
a=1
c = 4
;
) /*read*/
continue;
continue; for(; buf = fgets(buf, 1024, fp)
continue;
continue;
continue;


continue;
continue;




/*add new section and value*/文件名參數要帶擴展名;……可能還有些問題,如有BUG請大家提出, 謝~
段名、鍵名大小敏感;
段名可有空隔,如:;
輸入端名無需方括號,但中間有空隔的必須用雙引號:"ab cd";
支持前端空格,如:
t1=2
t2=other
可加入新的段;
将所有的“段名”和其在文件中“位置”放入一个“单/双向链表中