linux如何修改c⽂件内容,Linuxc配置⽂件读取与修改#include/*
*从配置⽂件中读取相应的值
*输⼊参数:1,配置⽂件路径 2,匹配标记 ,返回读取的变量天师捉奸
*并且排除了空⾏,“=”前后⽆内容,⽆“=”的情况
*/
char * 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,"rw");
if(f == NULL)
{
printf("Open configure file fail!
");
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) //去除最后⼀位是
的情况
{
memset(exchange_buf,0,sizeof(exchange_buf));
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)-1); break;
}
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf,0,sizeof(config_linebuf));
}
fclose(f);
return config_buff;
}各种花的开花时间
/*
*添加修改⽂件(当配置⽂件中存在标记字段,则进⾏修改,若不存在则进⾏添加)
*
*输⼊参数: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 configure fail!
");
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,conf_name);
strcat(sum_buf,"=");
strcat(sum_buf,config_buff);
strcat(sum_buf,"
");
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,"
");
}
printf("---configure---->
%s
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论