在一些场合,需要对一些配置文件进行读取,去设置软件的参数,自己实现了一些接口函数,以供以后使用。

ConfigFile.c

 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <direct.h>
#define MAX_LINE_LENGTH 256 int read_line(FILE *fp, char *bp)
{
char c = '\0';
int i = ;
bool isAssgin = ;
/* Read one line from the source file */
while ()
{
c = getc(fp);
if (c == '\n')
{
break;
} if (c == '\r')
{
continue;
} if (c == '=')
{
isAssgin = ;
} if (feof(fp))
{
/* return FALSE on unexpected EOF */
if (isAssgin == )
{
bp[i] = '\0';
return();
}
else
{
return();
}
}
bp[i++] = c;
}
bp[i] = '\0';
return();
}
/************************************************************************
* Function: Get_private_profile_int()
* Arguments: <char *> section - the name of the section to search for
* <char *> entry - the name of the entry to find the value of
* <int> def - the default value in the event of a failed read
* <char *> file_name - the name of the .ini file to read from
* Returns: the value located at entry
*************************************************************************/
int Get_private_profile_int(const char *section, const char *entry, int def, char *file_name)
{
FILE *fp = fopen(file_name, "r");
//Try to fix the issue that the MAX_PATH should be 256, not 80
char buff[MAX_LINE_LENGTH];
char *ep;
char t_section[MAX_LINE_LENGTH];
char value[];
int len = strlen(entry);
int i;
//To support negative number convert
bool b_IsNegative = false;
if (!fp)
{
return();
}
sprintf(t_section, "[%s]", section); /* Format the section name */
/* Move through file 1 line at a time until a section is matched or EOF */
do
{
if (!read_line(fp, buff))
{
fclose(fp);
return(def);
}
}while (strcmp(buff, t_section));
/* Now that the section has been found, find the entry.
* Stop searching upon leaving the section's area. */
do
{
if (!read_line(fp, buff) || buff[] == '[') //130516 Willy modify '\0' to '[' for parser ini bug.
{
fclose(fp);
return(def);
}
}while (strncmp(buff, entry, len)); ep = strrchr(buff, '='); /* Parse out the equal sign */
ep++;
if (!strlen(ep)) /* No setting? */
{
return(def);
}
/* Copy only numbers fail on characters */
//To support negative number convert
if (ep[] == '-')
{
b_IsNegative = true;
for (i = ; isdigit(ep[i]); i++)
{
value[i - ] = ep[i];
}
value[--i] = '\0';
}
else
{
for (i = ; isdigit(ep[i]); i++)
{
value[i] = ep[i];
}
value[i] = '\0';
}
fclose(fp); /* Clean up and return the value */
//To support negative number convert
if (b_IsNegative)
{
return ( - atoi(value));
}
else
{
return(atoi(value));
}
} unsigned long long Get_private_profile_longlong(const char *section, const char *entry, unsigned long long def, char *file_name)
{
FILE *fp = fopen(file_name, "r");
char buff[MAX_LINE_LENGTH];
char *ep;
char t_section[MAX_LINE_LENGTH];
char value[];
int len = strlen(entry);
int i;
if (!fp)
{
return();
}
sprintf(t_section, "[%s]", section); /* Format the section name */
/* Move through file 1 line at a time until a section is matched or EOF */
do
{
if (!read_line(fp, buff))
{
fclose(fp);
return(def);
}
}while (strcmp(buff, t_section));
/* Now that the section has been found, find the entry.
* Stop searching upon leaving the section's area. */
do
{
if (!read_line(fp, buff) || buff[] == '[') //130516 Willy modify '\0' to '[' for parser ini bug.
{
fclose(fp);
return(def);
}
}while (strncmp(buff, entry, len)); ep = strrchr(buff, '='); /* Parse out the equal sign */
ep++;
if (!strlen(ep)) /* No setting? */
{
return(def);
}
/* Copy only numbers fail on characters */
for (i = ; isdigit(ep[i]); i++)
{
value[i] = ep[i];
}
value[i] = '\0';
fclose(fp); /* Clean up and return the value */
return(_atoi64(value));
} /**************************************************************************
* Function: Get_private_profile_string()
* Arguments: <char *> section - the name of the section to search for
* <char *> entry - the name of the entry to find the value of
* <char *> def - default string in the event of a failed read
* <char *> buffer - a pointer to the buffer to copy into
* <int> buffer_len - the max number of characters to copy
* <char *> file_name - the name of the .ini file to read from
* Returns: the number of characters copied into the supplied buffer
***************************************************************************/
int Get_private_profile_string(const char *section, const char *entry, const char *def, char *buffer, int buffer_len, char *file_name)
{ FILE *fp = fopen(file_name, "r");
char buff[MAX_LINE_LENGTH];
char *ep;
char t_section[MAX_LINE_LENGTH];
int len = strlen(entry);
if (!fp)
{
return();
}
sprintf(t_section, "[%s]", section); /* Format the section name */
/* Move through file 1 line at a time until a section is matched or EOF */
do
{
if (!read_line(fp, buff))
{
strncpy(buffer, def, buffer_len);
return(strlen(buffer));
}
}while (strcmp(buff, t_section));
/* Now that the section has been found, find the entry.
* Stop searching upon leaving the section's area. */
do
{
if (!read_line(fp, buff) || buff[] == '[') //130516 Willy modify '\0' to '[' for parser ini bug.
{
fclose(fp);
strncpy(buffer, def, buffer_len);
return(strlen(buffer));
}
}while (strncmp(buff, entry, len)); ep = strrchr(buff, '='); /* Parse out the equal sign */
do
{
ep++;
}while(!strncmp(ep, " ", )); //Remove the blank space /* Copy up to buffer_len chars to buffer */
strncpy(buffer, ep, buffer_len - );
buffer[buffer_len - ] = '\0';
fclose(fp); /* Clean up and return the amount copied */
return(strlen(buffer));
}
int Get_private_profile_hex(const char *section, const char *entry, int def, char *file_name)
{
char valBuf[], valBuf2[];
int data; memset(valBuf, , sizeof(valBuf));
memset(valBuf2, , sizeof(valBuf2)); sprintf(valBuf2, "0x%x", def);
Get_private_profile_string(section, entry, valBuf2, &valBuf[], sizeof(valBuf), file_name);
data = ;
sscanf(valBuf, "0x%x", &data);
return data;
} /*************************************************************************
* Function: Write_private_profile_string()
* Arguments: <char *> section - the name of the section to search for
* <char *> entry - the name of the entry to find the value of
* <char *> buffer - pointer to the buffer that holds the string
* <char *> file_name - the name of the .ini file to read from
* Returns: TRUE if successful, otherwise FALSE
*************************************************************************/
int Write_private_profile_string(const char *section, const char *entry, char *buffer, char *file_name)
{
FILE *rfp, *wfp;
char tmp_name[];
//Try to fix the issue that the MAX_PATH should be 256, not 80
char buff[MAX_LINE_LENGTH];
char t_section[MAX_LINE_LENGTH];
int len = strlen(entry);
tmpnam(tmp_name); /* Get a temporary file name to copy to */
sprintf(t_section, "[%s]", section); /* Format the section name */ rfp = fopen(file_name, "r");
if (!rfp) /* If the .ini file doesn't exist */
{
wfp = fopen(file_name, "w");
if (!wfp) /* then make one */
{
return();
}
fprintf(wfp, "%s\n", t_section);
fprintf(wfp, "%s=%s\n", entry, buffer);
fclose(wfp);
return();
} wfp = fopen(tmp_name, "w");
if (!wfp)
{
fclose(rfp);
return();
} /* Move through the file one line at a time until a section is
* matched or until EOF. Copy to temp file as it is read. */
do
{
if (!read_line(rfp, buff))
{
/* Failed to find section, so add one to the end */
fprintf(wfp, "\n%s\n", t_section);
fprintf(wfp, "%s=%s\n", entry, buffer);
/* Clean up and rename */
fclose(rfp);
fclose(wfp);
unlink(file_name);
rename(tmp_name, file_name);
return();
}
fprintf(wfp, "%s\n", buff);
}while (strcmp(buff, t_section)); /* Now that the section has been found, find the entry. Stop searching
* upon leaving the section's area. Copy the file as it is read
* and create an entry if one is not found. */
while ()
{
if (!read_line(rfp, buff))
{
/* EOF without an entry so make one */
fprintf(wfp, "%s=%s\n", entry, buffer);
/* Clean up and rename */
fclose(rfp);
fclose(wfp);
unlink(file_name);
rename(tmp_name, file_name);
return(); }
if (!strncmp(buff, entry, len) || buff[] == '\0')
{
break;
}
fprintf(wfp, "%s\n", buff);
} if (buff[] == '\0')
{
fprintf(wfp, "%s=%s\n", entry, buffer);
do
{
fprintf(wfp, "%s\n", buff);
}
while (read_line(rfp, buff));
}
else
{
fprintf(wfp, "%s=%s\n", entry, buffer);
while (read_line(rfp, buff))
{
fprintf(wfp, "%s\n", buff);
}
}
/* Clean up and rename */
fclose(wfp);
fclose(rfp);
unlink(file_name);
rename(tmp_name, file_name);
return();
} int Write_private_profile_int(const char *section, const char *entry, int data, char *file_name)
{
char valBuf[];
memset(valBuf, , );
sprintf(valBuf, "%d", data);
return Write_private_profile_string(section, entry, valBuf, file_name);
} unsigned long long Write_private_profile_longlong(const char *section, const char *entry, unsigned long long data, char *file_name)
{
char valBuf[];
memset(valBuf, , );
sprintf(valBuf, "%Lu", data);
return Write_private_profile_string(section, entry, valBuf, file_name);
}

ConfigFile.h

 #ifndef _CONFIGFILE_H_
#define _CONFIGFILE_H_ extern int Get_private_profile_int(const char *section, const char *entry, int def, char *file_name);
extern unsigned long long Get_private_profile_longlong(const char *section, const char *entry, unsigned long long def, char *file_name);
extern int Get_private_profile_string(const char *section, const char *entry, const char *def, char *buffer, int buffer_len, char *file_name);
extern int Get_private_profile_hex(const char *section, const char *entry, int def, char *file_name); extern int Write_private_profile_string(const char *section, const char *entry, char *buffer, char *file_name);
extern int Write_private_profile_int(const char *section, const char *entry, int data, char *file_name);
extern unsigned long long Write_private_profile_longlong(const char *section, const char *entry, unsigned long long data, char *file_name); #endif //_CONFIGFILE_H_

测试:

当前目录下Autoconfig.ini文件的内容为

测试源码:main.c

 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <direct.h>
#include "GetConfig.h"
#define DEFAULT_INI_FILE "\\Autoconfig.ini" int main(int argc, char* argv[])
{
char ProductModel[];
char iniFile[];
char *buffer;
buffer = getcwd(NULL, );
strcpy(iniFile, buffer);
strcat(iniFile,DEFAULT_INI_FILE); Get_private_profile_string("Setting", "ProductModel", "SS", ProductModel, sizeof(ProductModel), iniFile);
printf("ProductModel:%s\n",ProductModel); unsigned char enExtendBlock = Get_private_profile_int("FwSetting", "EnExtendBlock", , iniFile);
printf("enExtendBlock:%d\n",enExtendBlock); int MPVersion = ;
Write_private_profile_int("Setting", "MPVersion", MPVersion, iniFile); sprintf(ProductModel,"ABCSFE!!221");
Write_private_profile_string("Setting", "ProductModel", ProductModel, iniFile); }

最新文章

  1. 第九回 Microsoft.Practices.Unity.Interception实现基于数据集的缓存(针对六,七,八讲的具体概念和配置的解说)
  2. OCJP(1Z0-851) 模拟题分析(四)over
  3. mysql忘记密码的重置方法
  4. Linux下命令sort, uniq
  5. &lt;十一&gt;面向对象分析之UML核心元素之组件
  6. 【随笔】这段时间没有写博客是因为一边看Qt5的帮助文档一边写小程序
  7. Linux下vim文件未正常退出,修复文件
  8. 用Linux命令行实现删除和复制指定类型的文件
  9. nodejs,http,get,post,请求
  10. iOS地理围栏技术的应用
  11. 02 基础设施/Gitlab - DevOps之路
  12. js正则表达式替换HTML标签以及空格(&amp;nbsp;)
  13. docker 删除镜像
  14. NOIP2012提高组day2 T2借教室
  15. Cannot locate BeanDefinitionParser for element [scoped-proxy]
  16. configparser模块(ini配置文件生成模块)
  17. Java Web之JSTL标准标签库总结
  18. Hook钩子编程
  19. win10系统的快捷键
  20. Oracle中rank() over, dense_rank(), row_number() 的区别

热门文章

  1. Python 再次改进版通过队列实现一个生产者消费者模型
  2. http --爬虫
  3. directive例子2
  4. SQL内外连
  5. Sping boot 之 @Value(&quot;${xxx}&quot;) 注解获取配置文件内容
  6. P1242 新汉诺塔(搜索+模拟退火)
  7. 错题:Test3
  8. review
  9. 【HAOI2012】外星人
  10. oracle命令导入SQL脚本