博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux读取按行读写文本文件
阅读量:7005 次
发布时间:2019-06-28

本文共 2177 字,大约阅读时间需要 7 分钟。

1.#include 
2.#include
3.#include
4.#include
5.#include
6.#include
7.8.9.typedef struct item_t {10. char *key;11. char *value;12.}ITEM;13.14./*15. *去除字符串右端空格16. */17.char *strtrimr(char *pstr)18.{19. int i;20. i = strlen(pstr) - 1;21. while (isspace(pstr[i]) && (i >= 0))22. pstr[i--] = '\0';23. return pstr;24.}25./*26. *去除字符串左端空格27. */28.char *strtriml(char *pstr)29.{30. int i = 0,j;31. j = strlen(pstr) - 1;32. while (isspace(pstr[i]) && (i <= j))33. i++;34. if (0
key = (char *)malloc(strlen(p) + 1);66. item->value = (char *)malloc(strlen(p2) + 1);67. strcpy(item->key,p);68. strcpy(item->value,p2);69.70. }71. return 0;//查询成功72.}73.74.int file_to_items(const char *file, struct item_t *items, int *num)75.{76. char line[1024];77. FILE *fp;78. fp = fopen(file,"r");79. if(fp == NULL)80. return 1;81. int i = 0;82. while(fgets(line, 1023, fp))83. {84. char *p = strtrim(line);85. int len = strlen(p);86. if(len <= 0)87. {88. continue;89. }90. else if(p[0]=='#')91. {92. continue;93. }94. else95. {96. char *p2 = strchr(p, '=');97. /*这里认为只有key没什么意义*/98. if(p2 == NULL)99. continue;100. *p2++ = '\0';101. items[i].key = (char *)malloc(strlen(p) + 1);102. items[i].value = (char *)malloc(strlen(p2) + 1);103. strcpy(items[i].key,p);104. strcpy(items[i].value,p2);105.106. i++;107. }108. }109. (*num) = i;110. fclose(fp);111. return 0;112.}113.114./*115. *读取value116. */117.int read_conf_value(const char *key,char *value1,const char *file)118.{119. char line[1024];120. char *key1,*key3,*key2;121. FILE *fp;122. fp = fopen(file,"r");123. if(fp == NULL)124. return 1;//文件打开错误125. while (fgets(line, 1023, fp)){126. ITEM item;127. get_item_from_line(line,&item);128. if(!strcmp(item.key,key)){129.130. strcpy(value1,item.value);131. fclose(fp);132. free(item.key);133. free(item.value);134. break;135. }136. }137. return 0;//成功138.139.}140.int write_conf_value(const char *key,char *value,const char *file)141.{142. ITEM items[20];// 假定配置项最多有20个143. int num;//存储从文件读取的有效数目144. file_to_items(file, items, &num);145.146. int i=0;147. //查找要修改的项148. for(i=0;i

  

转载地址:http://ekjtl.baihongyu.com/

你可能感兴趣的文章
node.js初学遇到的问题
查看>>
hibernate08--OpenSessionInView
查看>>
转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法...
查看>>
配置ORACLE 客户端连接到数据库
查看>>
drop asm disk、撤销drop asm disk
查看>>
Standby Redo Log 的设定原则、创建、删除、查看、归档位置
查看>>
[十二省联考2019]异或粽子
查看>>
winform 皮肤
查看>>
判断给定字符串中的大括号是否闭合
查看>>
MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型
查看>>
Python 练习
查看>>
Silverlight知识点
查看>>
步步为营:Asp.Net序列化与反序列化
查看>>
论php数组合并
查看>>
内网域名配置方法
查看>>
我的Visual Studio必用工具
查看>>
Java语法
查看>>
【转载】白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
查看>>
06抽象类
查看>>
LeetCode 26: Remove Duplicates from Sorted Array
查看>>