新聞中心
INI文件是一種常用的配置文件格式,它通常用于存儲(chǔ)程序的配置信息,比如窗口大小、用戶設(shè)置、數(shù)據(jù)庫(kù)配置等信息。在C語(yǔ)言中,讀取INI文件是一項(xiàng)必要的技能。下面將介紹。

公司主營(yíng)業(yè)務(wù):網(wǎng)站建設(shè)、做網(wǎng)站、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出衛(wèi)輝免費(fèi)做網(wǎng)站回饋大家。
一、什么是INI文件
INI是英文“Initialization”的縮寫,意為初始化。INI文件是一種普遍使用的文件類型,通常用于存儲(chǔ)程序的配置信息。INI文件的格式如下:
“`ini
[section1]
key1=value1
key2=value2
[section2]
key1=value1
key2=value2
“`
其中,方括號(hào)表示一個(gè)節(jié)(section),里面的是鍵值對(duì)(key-value pr),每個(gè)鍵值對(duì)之間用等號(hào)連接。
二、使用C語(yǔ)言讀取INI文件
C語(yǔ)言中常用的方法是使用標(biāo)準(zhǔn)庫(kù)函數(shù)fopen和fread,首先我們需要定義一個(gè)結(jié)構(gòu)體用來(lái)存儲(chǔ)INI文件中的鍵值對(duì)。
“`c
typedef struct INIItem {
char* key;
char* value;
} INIItem;
“`
接下來(lái),我們需要定義一個(gè)結(jié)構(gòu)體用來(lái)存儲(chǔ)INI文件中的節(jié)及其下面的鍵值對(duì)。
“`c
typedef struct INISection {
char* name;
int itemCount;
INIItem* items;
} INISection;
“`
其中,name表示節(jié)的名稱,itemCount表示該節(jié)下的鍵值對(duì)數(shù)量,items是一個(gè)指針,指向該節(jié)下的所有鍵值對(duì)。
然后,我們定義一個(gè)結(jié)構(gòu)體用來(lái)存儲(chǔ)整個(gè)INI文件。
“`c
typedef struct INIFile {
int sectionCount;
INISection* sections;
} INIFile;
“`
其中,sectionCount表示INI文件中節(jié)的數(shù)量,sections是一個(gè)指針,指向INI文件中的所有節(jié)。
我們需要定義一個(gè)函數(shù)來(lái)讀取INI文件,它的返回值是一個(gè)INIFile結(jié)構(gòu)體,傳入的參數(shù)是INI文件名。
“`c
INIFile readINIFile(const char* fileName) {
INIFile iniFile;
iniFile.sectionCount = 0;
iniFile.sections = NULL;
FILE* file = fopen(fileName, “r”);
if (file == NULL) {
return iniFile;
}
char line[256];
INISection* currentSection = NULL;
while (fgets(line, sizeof(line), file) != NULL) {
char* pos = line;
while (*pos == ‘ ‘ || *pos == ‘\t’) {
pos++;
}
if (*pos == ‘[‘) {
char* end = strchr(pos, ‘]’);
if (end != NULL) {
*end = ‘\0’;
INISection* section = malloc(sizeof(INISection));
section->name = strdup(pos + 1);
section->itemCount = 0;
section->items = NULL;
iniFile.sectionCount++;
iniFile.sections = realloc(iniFile.sections, iniFile.sectionCount * sizeof(INISection));
iniFile.sections[iniFile.sectionCount – 1] = *section;
currentSection = &(iniFile.sections[iniFile.sectionCount – 1]);
}
} else if (*pos != ‘\n’ && *pos != ‘\r’ && *pos != ‘#’) {
if (currentSection == NULL) {
INISection* section = malloc(sizeof(INISection));
section->name = strdup(“default”);
section->itemCount = 0;
section->items = NULL;
iniFile.sectionCount++;
iniFile.sections = realloc(iniFile.sections, iniFile.sectionCount * sizeof(INISection));
iniFile.sections[iniFile.sectionCount – 1] = *section;
currentSection = &(iniFile.sections[iniFile.sectionCount – 1]);
}
char* equals = strchr(pos, ‘=’);
if (equals != NULL) {
*equals = ‘\0’;
INIItem* item = malloc(sizeof(INIItem));
item->key = strdup(pos);
item->value = strdup(equals + 1);
currentSection->itemCount++;
currentSection->items = realloc(currentSection->items, currentSection->itemCount * sizeof(INIItem));
currentSection->items[currentSection->itemCount – 1] = *item;
}
}
}
fclose(file);
return iniFile;
}
“`
該函數(shù)先定義了一個(gè)名為iniFile的INIFile結(jié)構(gòu)體,并初始化了它的sectionCount為0。接下來(lái),使用fopen函數(shù)打開(kāi)文件,并對(duì)文件內(nèi)容進(jìn)行處理。文件讀取結(jié)束后,關(guān)閉文件,返回iniFile。
三、使用示例
以下是使用示例:
“`c
#include
#include
#include
typedef struct INIItem {
char* key;
char* value;
} INIItem;
typedef struct INISection {
char* name;
int itemCount;
INIItem* items;
} INISection;
typedef struct INIFile {
int sectionCount;
INISection* sections;
} INIFile;
INIFile readINIFile(const char* fileName) {
INIFile iniFile;
iniFile.sectionCount = 0;
iniFile.sections = NULL;
FILE* file = fopen(fileName, “r”);
if (file == NULL) {
return iniFile;
}
char line[256];
INISection* currentSection = NULL;
while (fgets(line, sizeof(line), file) != NULL) {
char* pos = line;
while (*pos == ‘ ‘ || *pos == ‘\t’) {
pos++;
}
if (*pos == ‘[‘) {
char* end = strchr(pos, ‘]’);
if (end != NULL) {
*end = ‘\0’;
INISection* section = malloc(sizeof(INISection));
section->name = strdup(pos + 1);
section->itemCount = 0;
section->items = NULL;
iniFile.sectionCount++;
iniFile.sections = realloc(iniFile.sections, iniFile.sectionCount * sizeof(INISection));
iniFile.sections[iniFile.sectionCount – 1] = *section;
currentSection = &(iniFile.sections[iniFile.sectionCount – 1]);
}
} else if (*pos != ‘\n’ && *pos != ‘\r’ && *pos != ‘#’) {
if (currentSection == NULL) {
INISection* section = malloc(sizeof(INISection));
section->name = strdup(“default”);
section->itemCount = 0;
section->items = NULL;
iniFile.sectionCount++;
iniFile.sections = realloc(iniFile.sections, iniFile.sectionCount * sizeof(INISection));
iniFile.sections[iniFile.sectionCount – 1] = *section;
currentSection = &(iniFile.sections[iniFile.sectionCount – 1]);
}
char* equals = strchr(pos, ‘=’);
if (equals != NULL) {
*equals = ‘\0’;
INIItem* item = malloc(sizeof(INIItem));
item->key = strdup(pos);
item->value = strdup(equals + 1);
currentSection->itemCount++;
currentSection->items = realloc(currentSection->items, currentSection->itemCount * sizeof(INIItem));
currentSection->items[currentSection->itemCount – 1] = *item;
}
}
}
fclose(file);
return iniFile;
}
int mn() {
INIFile iniFile = readINIFile(“example.ini”);
for (int i = 0; i
printf(“[%s]\n”, iniFile.sections[i].name);
for (int j = 0; j
printf(“%s=%s\n”, iniFile.sections[i].items[j].key, iniFile.sections[i].items[j].value);
}
printf(“\n”);
}
return 0;
}
“`
在以上示例中,我們讀取了文件example.ini,然后打印了文件中所有的節(jié)及其下面的鍵值對(duì)。
四、
相關(guān)問(wèn)題拓展閱讀:
- linux里怎樣修改 ini文件
linux里怎樣修改 ini文件
您好,提問(wèn)者:
cat xx.ini //查看文件內(nèi)容
vi xx.ini //修改內(nèi)容
提示:此時(shí)想要修改輸入i,然后移動(dòng)鍵盤進(jìn)行修改,如果想要保存請(qǐng)ESC 然后輸入:wq!
vi打開(kāi)文件改
關(guān)于c linux讀取ini文件的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都創(chuàng)新互聯(lián)科技有限公司,是一家專注于互聯(lián)網(wǎng)、IDC服務(wù)、應(yīng)用軟件開(kāi)發(fā)、網(wǎng)站建設(shè)推廣的公司,為客戶提供互聯(lián)網(wǎng)基礎(chǔ)服務(wù)!
創(chuàng)新互聯(lián)(www.cdcxhl.com)提供簡(jiǎn)單好用,價(jià)格厚道的香港/美國(guó)云服務(wù)器和獨(dú)立服務(wù)器。創(chuàng)新互聯(lián)成都老牌IDC服務(wù)商,專注四川成都IDC機(jī)房服務(wù)器托管/機(jī)柜租用。為您精選優(yōu)質(zhì)idc數(shù)據(jù)中心機(jī)房租用、服務(wù)器托管、機(jī)柜租賃、大帶寬租用,可選線路電信、移動(dòng)、聯(lián)通等。
網(wǎng)頁(yè)名稱:如何在C語(yǔ)言下讀取INI文件(clinux讀取ini文件)
文章來(lái)源:http://www.fisionsoft.com.cn/article/djpiejg.html


咨詢
建站咨詢
