linux读取按行读写文本文件
2015-05-21 23:19
225 查看
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct item_t {
char *key;
char *value;
}ITEM;
/*
*去除字符串右端空格
*/
char *strtrimr(char *pstr)
{
int i;
i = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i >= 0))
pstr[i--] = '\0';
return pstr;
}
/*
*去除字符串左端空格
*/
char *strtriml(char *pstr)
{
int i = 0,j;
j = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i <= j))
i++;
if (0<i)
strcpy(pstr, &pstr[i]);
return pstr;
}
/*
*去除字符串两端空格
*/
char *strtrim(char *pstr)
{
char *p;
p = strtrimr(pstr);
return strtriml(p);
}
/*
*从配置文件的一行读出key或value,返回item指针
*line--从配置文件读出的一行
*/
int get_item_from_line(char *line, struct
item_t *item)
{
char *p = strtrim(line);
int len = strlen(p);
if(len <= 0){
return 1;//空行
}
else if(p[0]=='#'){
return 2;
}else{
char *p2 = strchr(p, '=');
*p2++ = '\0';
item->key = (char *)malloc(strlen(p) + 1);
item->value = (char *)malloc(strlen(p2) + 1);
strcpy(item->key,p);
strcpy(item->value,p2);
}
return 0;//查询成功
}
int file_to_items(const char *file, struct
item_t *items, int *num)
{
char line[1024];
FILE *fp;
fp = fopen(file,"r");
if(fp == NULL)
return 1;
int i = 0;
while(fgets(line, 1023, fp))
{
char *p = strtrim(line);
int len = strlen(p);
if(len <= 0)
{
continue;
}
else if(p[0]=='#')
{
continue;
}
else
{
char *p2 = strchr(p, '=');
/*这里认为只有key没什么意义*/
if(p2 == NULL)
continue;
*p2++ = '\0';
items[i].key = (char *)malloc(strlen(p) + 1);
items[i].value = (char *)malloc(strlen(p2) + 1);
strcpy(items[i].key,p);
strcpy(items[i].value,p2);
i++;
}
}
(*num) = i;
fclose(fp);
return 0;
}
/*
*读取value
*/
int read_conf_value(const char *key,char *value1,const char *file)
{
char line[1024];
char *key1,*key3,*key2;
FILE *fp;
fp = fopen(file,"r");
if(fp == NULL)
return 1;//文件打开错误
while (fgets(line, 1023, fp)){
ITEM item;
get_item_from_line(line,&item);
if(!strcmp(item.key,key)){
strcpy(value1,item.value);
fclose(fp);
free(item.key);
free(item.value);
break;
}
}
return 0;//成功
}
int write_conf_value(const char *key,char *value,const char *file)
{
ITEM items[20];// 假定配置项最多有20个
int num;//存储从文件读取的有效数目
file_to_items(file, items, &num);
int i=0;
//查找要修改的项
for(i=0;i<num;i++){
if(!strcmp(items[i].key, key)){
items[i].value = value;
break;
}
}
// 更新配置文件,应该有备份,下面的操作会将文件内容清除
FILE *fp;
fp = fopen(file, "w");
if(fp == NULL)
return 1;
i=0;
for(i=0;i<num;i++){
fprintf(fp,"%s=%s\n",items[i].key, items[i].value);
//printf("%s=%s\n",items[i].key, items[i].value);
}
fclose(fp);
//清除工作
/* i=0;
for(i=0;i<num;i++){
free(items[i].key);
free(items[i].value);
}*/
return 0;
}
void main(void)
{
char *key;
char *value=NULL,*value1=NULL;
char *file;
file="/home/wangwei/ww/file/from_file";
key="IP";
value=(char *)malloc(sizeof(char)*30);
value1=(char *)malloc(sizeof(char)*30);
read_conf_value(key,value,file);
printf("IP = %s\n",value);
key="MASK";
read_conf_value(key,value,file);
printf("MASK = %s\n",value);
key="GATEWAY";
read_conf_value(key,value,file);
printf("GATEWAY = %s\n",value);
free(value);
free(value1);
value=NULL;
value1=NULL;
}
FROM: http://blog.chinaunix.net/uid-26921272-id-3328858.html
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct item_t {
char *key;
char *value;
}ITEM;
/*
*去除字符串右端空格
*/
char *strtrimr(char *pstr)
{
int i;
i = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i >= 0))
pstr[i--] = '\0';
return pstr;
}
/*
*去除字符串左端空格
*/
char *strtriml(char *pstr)
{
int i = 0,j;
j = strlen(pstr) - 1;
while (isspace(pstr[i]) && (i <= j))
i++;
if (0<i)
strcpy(pstr, &pstr[i]);
return pstr;
}
/*
*去除字符串两端空格
*/
char *strtrim(char *pstr)
{
char *p;
p = strtrimr(pstr);
return strtriml(p);
}
/*
*从配置文件的一行读出key或value,返回item指针
*line--从配置文件读出的一行
*/
int get_item_from_line(char *line, struct
item_t *item)
{
char *p = strtrim(line);
int len = strlen(p);
if(len <= 0){
return 1;//空行
}
else if(p[0]=='#'){
return 2;
}else{
char *p2 = strchr(p, '=');
*p2++ = '\0';
item->key = (char *)malloc(strlen(p) + 1);
item->value = (char *)malloc(strlen(p2) + 1);
strcpy(item->key,p);
strcpy(item->value,p2);
}
return 0;//查询成功
}
int file_to_items(const char *file, struct
item_t *items, int *num)
{
char line[1024];
FILE *fp;
fp = fopen(file,"r");
if(fp == NULL)
return 1;
int i = 0;
while(fgets(line, 1023, fp))
{
char *p = strtrim(line);
int len = strlen(p);
if(len <= 0)
{
continue;
}
else if(p[0]=='#')
{
continue;
}
else
{
char *p2 = strchr(p, '=');
/*这里认为只有key没什么意义*/
if(p2 == NULL)
continue;
*p2++ = '\0';
items[i].key = (char *)malloc(strlen(p) + 1);
items[i].value = (char *)malloc(strlen(p2) + 1);
strcpy(items[i].key,p);
strcpy(items[i].value,p2);
i++;
}
}
(*num) = i;
fclose(fp);
return 0;
}
/*
*读取value
*/
int read_conf_value(const char *key,char *value1,const char *file)
{
char line[1024];
char *key1,*key3,*key2;
FILE *fp;
fp = fopen(file,"r");
if(fp == NULL)
return 1;//文件打开错误
while (fgets(line, 1023, fp)){
ITEM item;
get_item_from_line(line,&item);
if(!strcmp(item.key,key)){
strcpy(value1,item.value);
fclose(fp);
free(item.key);
free(item.value);
break;
}
}
return 0;//成功
}
int write_conf_value(const char *key,char *value,const char *file)
{
ITEM items[20];// 假定配置项最多有20个
int num;//存储从文件读取的有效数目
file_to_items(file, items, &num);
int i=0;
//查找要修改的项
for(i=0;i<num;i++){
if(!strcmp(items[i].key, key)){
items[i].value = value;
break;
}
}
// 更新配置文件,应该有备份,下面的操作会将文件内容清除
FILE *fp;
fp = fopen(file, "w");
if(fp == NULL)
return 1;
i=0;
for(i=0;i<num;i++){
fprintf(fp,"%s=%s\n",items[i].key, items[i].value);
//printf("%s=%s\n",items[i].key, items[i].value);
}
fclose(fp);
//清除工作
/* i=0;
for(i=0;i<num;i++){
free(items[i].key);
free(items[i].value);
}*/
return 0;
}
void main(void)
{
char *key;
char *value=NULL,*value1=NULL;
char *file;
file="/home/wangwei/ww/file/from_file";
key="IP";
value=(char *)malloc(sizeof(char)*30);
value1=(char *)malloc(sizeof(char)*30);
read_conf_value(key,value,file);
printf("IP = %s\n",value);
key="MASK";
read_conf_value(key,value,file);
printf("MASK = %s\n",value);
key="GATEWAY";
read_conf_value(key,value,file);
printf("GATEWAY = %s\n",value);
free(value);
free(value1);
value=NULL;
value1=NULL;
}
FROM: http://blog.chinaunix.net/uid-26921272-id-3328858.html
相关文章推荐
- linux读取按行读写文本文件
- 嵌入式 linux读取按行读写文本文件
- linux读取按行读写文本文件
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- 读写文本文件 ---字符行式读取
- 文本文件读取和二进制文件读写的区别
- 对大数据文本文件读取(按行读取)的优化 3ff7
- linux读取文本文件 GBK转utf-8
- python直接按行读取gz压缩文件中的文本文件的数据
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- linux按行读取一个文件,然后输出
- linux 文件读写 读取文件大小
- Java按行读取文本文件并保存
- 文件磁盘相关函数[23]-文本文件读写-不同数据类型的读取
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- linux下使用read函数实现的按行读取readline
- PHP快速按行读取CSV大文件的封装类分享(也适用于其它超大文本文件)
- Linux下读取UTF-8文本文件
- 嵌入式 Linux按行读取文件