相关博文:
http://www.doc88.com/p-632738089005.html
http://www.cnblogs.com/hoys/archive/2011/04/25/2026828.html
http://www.chinaunix.net//jh/23/153353.html
http://www.linuxidc.com/Linux/2011-03/33712.htm
/*
*从配置文件中读取相应的值
*输入参数:1,配置文件路径 2,匹配标记 3,输出存储空间
*并且排除了空行,“=”前后无内容,无“=”的情况
*/
ReadConfig(char *conf_path,char *conf_name,char *config_buff)
{
char config_linebuf[256];
char line_name[40];
char exchange_buf[256];
char *config_sign = "=";
char *leave_line;
FILE *f;
f = fopen(conf_path,"r");
if(f == NULL)
{
printf("OPEN CONFIG FALID\n");
return 0;
}
fseek(f,0,SEEK_SET);
while(fgets(config_linebuf,256,f) != NULL)
{
if(strlen(config_linebuf) < 3) //判断是否是空行
{
continue;
}
if (config_linebuf[strlen(config_linebuf)-1] == 10) //去除最后一位是\n的情况
{
memset(exchange_buf,0,sizeof(exchange_buf));
strncpy(exchange_buf,config_linebuf,strlen(config_linebuf)-1);
memset(config_linebuf,0,sizeof(config_linebuf));
strcpy(config_linebuf,exchange_buf);
}
memset(line_name,0,sizeof(line_name));
leave_line = strstr(config_linebuf,config_sign);
if(leave_line == NULL) //去除无"="的情况
{
continue;
}
int leave_num = leave_line - config_linebuf;
strncpy(line_name,config_linebuf,leave_num);
if(strcmp(line_name,conf_name) ==0)
{
strncpy(config_buff,config_linebuf+(leave_num+1),strlen(config_linebuf)-leave_num-1);
break;
}
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
fclose(f);
}
/*
*添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加)
*
*输入参数:1,配置文件路径 2,匹配标记 3,替换或添加的内容
*
*/
AddOrAltConfig(char *conf_path,char *conf_name,char *config_buff)
{
char config_linebuf[256];
char line_name[40];
char *config_sign = "=";
char *leave_line;
int alter_sign = 0;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
printf("OPEN CONFIG FALID\n");
return 0;
}
fseek(f,0,SEEK_END);
long congig_lenth = ftell(f);
int configbuf_lenth = strlen(config_buff);
configbuf_lenth = configbuf_lenth + 5;
char sum_buf[congig_lenth+configbuf_lenth];
memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET);
while(fgets(config_linebuf,256,f) != NULL)
{
if(strlen(config_linebuf) < 3) //判断是否是空行
{
strcat(sum_buf,config_linebuf);
continue;
}
leave_line = NULL;
leave_line = strstr(config_linebuf,config_sign);
if(leave_line == NULL) //去除无"="的情况
{
strcat(sum_buf,config_linebuf);
continue;
}
int leave_num = leave_line - config_linebuf;
memset(line_name,0,sizeof(line_name));
strncpy(line_name,config_linebuf,leave_num);
if(strcmp(line_name,conf_name) ==0)
{
strcat(sum_buf,config_buff);
strcat(sum_buf,"\n");
alter_sign = 1;
}
else
{
strcat(sum_buf,config_linebuf);
}
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
if(alter_sign == 0)
{
strcat(sum_buf,config_buff);
strcat(sum_buf,"\n");
}
printf("---sum_buf---->%s<----------\n",sum_buf);
remove(conf_path);
fclose(f);
FILE *fp;
fp = fopen(conf_path,"w+");
if(fp == NULL)
{
printf("OPEN CONFIG FALID\n");
return 2;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
}
/*
*删除配置文件内容(
*
*输入参数:1,配置文件路径 2,匹配标记
*
*/
DeleteConfig(char *conf_path,char *conf_name)
{
char config_linebuf[256];
char line_name[40];
char *config_sign = "=";
char *leave_line;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
printf("OPEN CONFIG FALID\n");
return 0;
}
fseek(f,0,SEEK_END);
long congig_lenth = ftell(f);
char sum_buf[congig_lenth+2];
memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET);
while(fgets(config_linebuf,256,f) != NULL)
{
if(strlen(config_linebuf) < 3) //判断是否是空行
{
strcat(sum_buf,config_linebuf);
continue;
}
leave_line = NULL;
leave_line = strstr(config_linebuf,config_sign);
if(leave_line == NULL) //去除无"="的情况
{
strcat(sum_buf,config_linebuf);
continue;
}
int leave_num = leave_line - config_linebuf;
memset(line_name,0,sizeof(line_name));
strncpy(line_name,config_linebuf,leave_num);
if(strcmp(line_name,conf_name) ==0)
{
}
else
{
strcat(sum_buf,config_linebuf);
}
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
printf("---sum_buf---->%s<----------\n",sum_buf);
remove(conf_path);
fclose(f);
FILE *fp;
fp = fopen(conf_path,"w+");
if(fp == NULL)
{
printf("OPEN CONFIG FALID\n");
return 2;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-03/33712.htm
/ ********************************************* FileName: FileScan.c Describe: Functions For Read Config File Author: Eboy Date: 2003.8.28 ****************************************************/ #ifdef _WINDOWS_ #include <windows.h>; #include <winbase.h>; #endif #include <stdio.h>; #include <stdlib.h>; #include <string.h>; #define CONF_FILE_MAX_LINE 50 #define CONF_FILE_MAX_ROW 100 /* * 函数名:IsBreakChar() * 功能: 判断是否为行结束符 * 返回值:1表示真,0表示假 * 参数说明:char ch 需要判断的字符 * */ int IsBreakChar(char ch) { if(ch==9) //Tab Key return 0; if((ch<32)&&(ch>;0)) return 1; else return 0; } /* * 函数名:ReadLine() * 功能: 从文件中读出一行 * 参数说明:FILE* fpt 文件描述符 * char* linestr 输出数据,结尾为'\0'的字符串 */ int ReadLine(FILE* fpt, char *linestr) { int i,rv; char cc; char buf[CONF_FILE_MAX_ROW]; if(feof(fpt)){ return 1; } for(i=0;i<CONF_FILE_MAX_ROW;i++){ rv=fread(&cc,1,1,fpt); if(rv<1) break; if(IsBreakChar(cc)==1) break; buf=cc; } buf='\0'; strcpy(linestr,buf); return 0; } /* * 函数名:TrimSpace() * 功能: 将字符串中的空格删除 * 参数说明:char_PTR ori 输入字符串 * char * trim 输出字符串 */ int TrimSpace(char * ori,char * trim) { int i,j,qq,len; char tmp[CONF_FILE_MAX_ROW]; j=qq=0; len=strlen(ori); for(i=0;i<len;i++){ if(qq==1) tmp[j]=ori; else{ if(ori<33) continue; else{ tmp[j]=ori; qq=1; } } j++; } tmp[j]='\0'; j=qq=0; for(i=strlen(tmp)-1;i>;=0;i--){ if(qq==1) trim[j]=tmp; else{ if(tmp<33) continue; else{ trim[j]=tmp; qq=1; } } j++; } trim[j]='\0'; j=strlen(trim); for(i=0;i<j;i++){ tmp=trim[j-i-1]; } memcpy(trim,tmp,j); return 0; } /* * 函数名:ReadSector() * 功能: 从输入字符串中分解出指定项的值 * 返回值: 如果成功返回0 * 参数说明:char * line 输入字符串 * char * SectName 指定域的名称 * char * SectVa 输出指定域的值(返回成功时有效) */ int ReadSector(char * line,char * SectName,char * SectVal) { int i; int tlen,equpos=0; char lineBuf[CONF_FILE_MAX_ROW]; char oriSectName[CONF_FILE_MAX_ROW],tmpSectName[CONF_FILE_MAX_ROW]; char oriSectVal[CONF_FILE_MAX_ROW]; TrimSpace(line,lineBuf); tlen=strlen(lineBuf); for(i=0;i<tlen;i++){ if((lineBuf=='#')||(lineBuf==';')) break; } if(i==0) return 1; lineBuf='\0'; tlen=strlen(lineBuf); for(i=0;i<tlen;i++){ if(lineBuf=='='){ equpos=i; break; } } if(equpos==0) return 1; memcpy(oriSectName,lineBuf,equpos); oriSectName[equpos]='\0'; TrimSpace(oriSectName,tmpSectName); if((SectName[0]=='#')||(SectName[0]==';')){ return 1; } if(strcmp(tmpSectName,SectName)) return 1; memcpy(oriSectVal,lineBuf+equpos+1,tlen-equpos-1); oriSectVal[tlen-equpos-1]='\0'; TrimSpace(oriSectVal,SectVal); return 0; } /* * 函数名:AnalizeConfFile() * 功能: 从指定文件中分解出指定域的值 * 返回值: 如果成功返回0 * 参数说明:char * Filename 指定配置文件名 * char * SectName 指定域的名称 * char * SectVa 输出指定域的值(返回成功时有效) */ int AnalizeConfFile(char * Filename,char * sector,char * sectval) { int i,rv,flag=0; FILE *fp; char line[CONF_FILE_MAX_LINE]; char syspath[256]; #ifdef _WINDOWS_ if(Filename!=NULL) strcpy(syspath,Filename); if(Filename == NULL) { if ((rv = GetSystemDirectory(syspath,256))<=0) { ErrorLocation; return 1; } if (rv >; 3) strcat(syspath,"/MyFile.ini"); else strcat(syspath,"MyFile.ini"); //#endif } #else if(Filename != NULL) { strcpy(syspath,Filename); } if(Filename ==NULL) { strcpy(syspath,"/etc/MyFile.ini"); } #endif fp=fopen(syspath,"r"); if(fp==NULL) { return 1; } for(i=0;i<CONF_FILE_MAX_LINE;i++) { rv = ReadLine(fp,line); if(rv != 0) break; if(ReadSector(line,sector,sectval)==0) { flag = 1; break; } } if (flag == 0) return -1; fclose(fp); return 0; } 使用时直接调用AnalizeConfFile函数即可。 原文链接:http://www.chinaunix.net//jh/23/153353.html