我正在使用iniparser
为1.Program使用ini文件,创建文件并生成其名称,这很好用。我有2.Program,应该从我试图使用inifile的第一个程序“得到一些怎么样”,但它不起作用,这里是ini的一个例子:
#
# This is an example of ini file
#
[RECORDING]
sampleFrequency = 44100 ; # Sample Frequency
folderName = RECORDS ; # Name of the folder, where the records will be saved
lengthInSec = 5 ; # the duration of the recording in Seconds
...............
1.程序:
iniparser_set(ini,"RECORDING:fileName","WHATEVER");// this doesn't work nothing happens not even an warning
任何想法我怎么能这样做。
更新
** ** jxh **回答这里是我尝试过的:
FILE *finp= NULL;
finp=fopen("initialization.ini","w");
dictionary * ini ;
iniparser_dump_ini(ini,finp); /// the program crashes here
iniparser_set(ini,"RECORDING:fileName","WHATEVER");
iniparser_dump(ini, stdout);
fclose(finp);
并且没有写入文件
答案 0 :(得分:2)
来自iniparser
文档:
void iniparser_dump_ini (dictionary *d,
FILE *f
)
将字典保存到可加载的ini文件中。
因此,您需要使用fopen()
打开一个可写文件,并将指针传递给iniparser_dump_ini()
以写出您的字典。然后fclose()
文件。
图书馆的设计主要是围绕阅读" ini"归档到dictionary
。 set和unset命令仅影响内存中的数据结构。
您的更新尝试使用未初始化的变量ini
,因此您将获得未定义的行为。
如前所述,图书馆显然是围绕阅读" ini"文件。所以,你需要从一个" ini"先归档。如果您必须从C代码执行所有操作,请从空文件开始:
const char ininame[] = "initialization.ini";
FILE *finp = fopen(ininame, "w");
fclose(finp);
然后,初始化字典。
dictionary *ini = iniparser_load(inifile);
然后对字典做任何你想做的事。
iniparser_set(ini,"RECORDING:fileName","WHATEVER");
然后把它写出来:
finp = fopen(inifile, "w");
iniparser_dump_ini(ini, finp);
fclose(finp);
答案 1 :(得分:2)
这两个函数应该保存并读取ini文件。此示例使用工作文件夹,但您可以提供绝对路径,例如" /home/some-user/some-folder/some-program.ini"。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// module variables
int iFoundTopic;
int iFoundItem;
int iError;
long lTopicFilePos;
char acTopicHeading[80];
char acLastTopicHeading[80];
char acItemHeading[80];
char acIniLine[160];
char acIniPath[160];
char acTempPath[160];
FILE *pFIniFile;
FILE *pFTempIni;
// saveinis
// save configuration string
int saveinis ( char *pacPath, char *pacTopic, char *pacItem, char *pacValue) {
int iItemLength;
int iValueLength;
iError = 0;
acLastTopicHeading[0] = '\0';
strcpy ( acIniPath, pacPath);
strcpy ( acTempPath, pacPath);
strcat ( acTempPath, "temp");
// add brackets to topic
strcpy ( acTopicHeading, "[");
strcat ( acTopicHeading, pacTopic);
strcat ( acTopicHeading, "]\n");
strcpy ( acItemHeading, pacItem);
strcat ( acItemHeading, "=");
iItemLength = strlen ( acItemHeading);
iValueLength = strlen ( pacValue);
iFoundTopic = 0;
iFoundItem = 0;
if ( ( pFTempIni = fopen ( acTempPath, "w")) == NULL) {
printf ( "could not open temp ini file to write settings\n");
iError = 1;
return ( iError);
}
// try to open current config file for reading
if ( ( pFIniFile = fopen ( acIniPath, "r")) != NULL) {
// read a line from the config file until EOF
while ( fgets ( acIniLine, 159, pFIniFile) != NULL) {
// the item has been found so continue reading file to end
if ( iFoundItem == 1) {
fputs ( acIniLine, pFTempIni);
continue;
}
// topic has not been found yet
if ( iFoundTopic == 0) {
if ( strcmp ( acTopicHeading, acIniLine) == 0) {
// found the topic
iFoundTopic = 1;
}
fputs ( acIniLine, pFTempIni);
continue;
}
// the item has not been found yet
if ( ( iFoundItem == 0) && ( iFoundTopic == 1)) {
if ( strncmp ( acItemHeading, acIniLine, iItemLength) == 0) {
// found the item
iFoundItem = 1;
if ( iValueLength > 0) {
fputs ( acItemHeading, pFTempIni);
fputs ( pacValue, pFTempIni);
fputs ( "\n", pFTempIni);
}
continue;
}
// if newline or [, the end of the topic has been reached
// so add the item to the topic
if ( ( acIniLine[0] == '\n') || ( acIniLine[0] == '[')) {
iFoundItem = 1;
if ( iValueLength > 0) {
fputs ( acItemHeading, pFTempIni);
fputs ( pacValue, pFTempIni);
fputs ( "\n", pFTempIni);
}
fputs ( "\n", pFTempIni);
if ( acIniLine[0] == '[') {
fputs ( acIniLine, pFTempIni);
}
continue;
}
// if the item has not been found, write line to temp file
if ( iFoundItem == 0) {
fputs ( acIniLine, pFTempIni);
continue;
}
}
}
fclose ( pFIniFile);
}
// still did not find the item after reading the config file
if ( iFoundItem == 0) {
// config file does not exist
// or topic does not exist so write to temp file
if ( iValueLength > 0) {
if ( iFoundTopic == 0) {
fputs ( acTopicHeading, pFTempIni);
}
fputs ( acItemHeading, pFTempIni);
fputs ( pacValue, pFTempIni);
fputs ( "\n\n", pFTempIni);
}
}
fclose ( pFTempIni);
//delete the ini file
remove ( acIniPath);
// rename the temp file to ini file
rename ( acTempPath, acIniPath);
return ( iError);
}
// readinis
// read configuration string
int readinis ( char *pacPath, char *pacTopic, char *pacItem, char *pacValue) {
int iItemLength;
int iValueLength;
char *pcLastCharacter;
iError = 0;
strcpy ( acIniPath, pacPath);
// add brackets to topic
strcpy ( acTopicHeading, "[");
strcat ( acTopicHeading, pacTopic);
strcat ( acTopicHeading, "]\n");
strcpy ( acItemHeading, pacItem);
strcat ( acItemHeading, "=");
iItemLength = strlen ( acItemHeading);
iFoundTopic = 0;
iFoundItem = 0;
// try to open current config file for reading
if ( ( pFIniFile = fopen ( acIniPath, "r")) != NULL) {
// has the topic been found before
if ( strcmp ( acLastTopicHeading, acTopicHeading) == 0) {
iFoundTopic = 1;
fseek ( pFIniFile, lTopicFilePos, SEEK_SET);
}
// read a line from the config file until EOF
while ( fgets ( acIniLine, 159, pFIniFile) != NULL) {
// topic has not been found yet
if ( iFoundTopic == 0) {
if ( strcmp ( acTopicHeading, acIniLine) == 0) {
// found the topic
iFoundTopic = 1;
lTopicFilePos = ftell ( pFIniFile);
strcpy ( acLastTopicHeading, acTopicHeading);
}
continue;
}
// the item has not been found yet
if ( ( iFoundItem == 0) && ( iFoundTopic == 1)) {
// if newline or [, the end of the topic has been reached
// no config value in file yet
if ( ( acIniLine[0] == '\n') || ( acIniLine[0] == '[')) {
iFoundItem = 1;
break;
}
if ( strncmp ( acItemHeading, acIniLine, iItemLength) == 0) {
// found the item
iFoundItem = 1;
strcpy ( pacValue, &acIniLine[iItemLength]);
continue;
}
}
}
fclose ( pFIniFile);
}
// remove trailing comment
iValueLength = strlen ( pacValue);
while ( iValueLength) {
if ( *(pacValue + iValueLength) == '#') {
*(pacValue + iValueLength) = '\0';
}
iValueLength--;
}
// remove trailing white space
while ( ( iValueLength = strlen ( pacValue)) > 0) {
pcLastCharacter = ( pacValue + iValueLength - 1);
if ( ( *pcLastCharacter == ' ')
|| ( *pcLastCharacter == '\n')
|| ( *pcLastCharacter == '\r')
|| ( *pcLastCharacter == '\t')
|| ( *pcLastCharacter == '\v')
|| ( *pcLastCharacter == '\a')
|| ( *pcLastCharacter == '\b')
|| ( *pcLastCharacter == '\f') ) {
*pcLastCharacter = '\0';
}
else {
break;
}
}
return ( iError);
}
int main ( ) {
char acValue[160];
acLastTopicHeading[0] = '\0'; // initialize
saveinis ( "program.ini", "FOLDER", "DEFAULT", "home");
saveinis ( "program.ini", "FOLDER", "WORKING", "downloads");
saveinis ( "program.ini", "FILES", "RECENT", "program#the most recent file is program");
saveinis ( "program.ini", "FILES", "WORKING", "program");
saveinis ( "program.ini", "FILES", "INI", "program.ini#do not delete program.ini");
readinis ( "program.ini", "FOLDER", "DEFAULT", acValue);
printf ( "%s\n", acValue);
readinis ( "program.ini", "FILES", "INI", acValue);
printf ( "%s\n", acValue);
readinis ( "program.ini", "FILES", "RECENT", acValue);
printf ( "%s\n", acValue);
readinis ( "program.ini", "FOLDER", "WORKING", acValue);
printf ( "%s\n", acValue);
readinis ( "program.ini", "FILES", "WORKING", acValue);
printf ( "%s\n", acValue);
return 0;
}
创建项目后,例如[FILES] RECENT =,可以在项目之前添加以#开头的注释。