免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1523 | 回复: 6
打印 上一主题 下一主题

[C] C 对文件内容操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-23 15:14 |只看该作者 |倒序浏览
大家好:
       我想对一个文件a操作,a的内容是:
                     a=1,b=2
                     c=3
                     d=4
         1.  现在想把c 的值打印出来,也就是3,请问怎么弄?
      2.  把c 的值变成10,既a的内容是:
              a=1,b=2
                     c=10
                     d=4
             怎么弄?
不用shell,请大家指点一下!谢谢

论坛徽章:
0
2 [报告]
发表于 2009-03-23 15:21 |只看该作者
忘了补充了,尽量能做到通用性,因为a文件里可能还会有关其他的带a字符

论坛徽章:
0
3 [报告]
发表于 2009-03-23 15:22 |只看该作者
APUE 第3章和第5章,看完了你就会了

论坛徽章:
0
4 [报告]
发表于 2009-03-23 16:04 |只看该作者
用open,read,write这些文件操作系统调用

论坛徽章:
0
5 [报告]
发表于 2009-03-23 16:06 |只看该作者
Shell是最偷懒的方式

选择C就只能自己动手

论坛徽章:
0
6 [报告]
发表于 2009-03-23 16:58 |只看该作者
没有人能给在下举个例子,抛砖引玉一下吗?

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
7 [报告]
发表于 2009-03-23 17:59 |只看该作者

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. struct s_exp {
  6.         struct s_exp *previous, *next;
  7.         char *name;
  8.         long value;
  9. };

  10. int get_char(FILE *fp, long offset)
  11. {
  12.         char c;

  13.         if (fseek(fp, offset, SEEK_SET) == 0) {
  14.                 if (fread(&c, 1, 1, fp) == 1) {
  15.                         return c;
  16.                 }
  17.         }
  18.         return EOF;
  19. }

  20. int get_name_length(FILE *fp, long offset)
  21. {
  22.         int n;
  23.         int c;

  24.         for (n = 0; (c = get_char(fp, offset)) != EOF; n++) {
  25.                 if (c == '=') {
  26.                         break;
  27.                 }
  28.                 offset++;
  29.         }
  30.         return n;
  31. }

  32. void trim_right(char *s)
  33. {
  34.         int len;

  35.         for (len = strlen(s) - 1; len >= 0; len--) {
  36.                 if (!isspace(s[len])) {
  37.                         s[len + 1] = '\0';
  38.                         break;
  39.                 }
  40.         }
  41. }

  42. int get_name(FILE *fp, long *offset, char *buff, int len)
  43. {
  44.         long i;
  45.         int n;
  46.         int c;

  47.         i = *offset;
  48.         for (n = 0; (c = get_char(fp, i)) != EOF && n < len; n++) {
  49.                 if (c == '=') {
  50.                         buff[n] = '\0';
  51.                         trim_right(buff);
  52.                         break;
  53.                 }
  54.                 buff[n] = c;
  55.                 i++;
  56.         }
  57.         *offset = i;
  58.         return 0;
  59. }

  60. int get_text(FILE *fp, long *offset, const char *text)
  61. {
  62.         long i;
  63.         int c;

  64.         i = *offset;
  65.         while (*text != '\0') {
  66.                 c = get_char(fp, i);
  67.                 if (c != EOF) {
  68.                         if (c == *text) {
  69.                                 text++;
  70.                                 i++;
  71.                                 continue;
  72.                         }
  73.                 }
  74.                 return -1;
  75.         }
  76.         *offset = i;
  77.         return 0;
  78. }

  79. int get_space(FILE *fp, long *offset)
  80. {
  81.         int i;
  82.         int c;

  83.         i = *offset;
  84.         c = get_char(fp, i);
  85.         if (c != EOF) {
  86.                 if (c == ' ' || c == '\t') {
  87.                         while ((c = get_char(fp, ++i)) != EOF && c == ' ' || c == '\t') {
  88.                         }
  89.                         *offset = i;
  90.                 }
  91.                 return 0;
  92.         }
  93.         return -1;
  94. }

  95. int get_return(FILE *fp, long *offset)
  96. {
  97.         if (get_text(fp, offset, "\r\n") == 0) {
  98.                 return 0;
  99.         }
  100.         return -1;
  101. }

  102. int get_long(FILE *fp, long *offset, long *value)
  103. {
  104.         long i;
  105.         long val;
  106.         int c;

  107.         i = *offset;
  108.         c = get_char(fp, i);
  109.         if (c != EOF) {
  110.                 if (isdigit(c)) {
  111.                         val = c - '0';
  112.                         while ((c = get_char(fp, ++i)) != EOF && isdigit(c)) {
  113.                                 val = val * 10 + c - '0';
  114.                         }
  115.                         *value = val;
  116.                         *offset = i;
  117.                 }
  118.                 return 0;
  119.         }
  120.         return -1;
  121. }

  122. struct s_exp *search_exp(struct s_exp **list, const char *name)
  123. {
  124.         struct s_exp *node;

  125.         if (*list != NULL) {
  126.                 node = *list;
  127.                 do {
  128.                         if (strcmp(node->name, name) == 0) {
  129.                                 return node;
  130.                         }
  131.                         node = node->next;
  132.                 }while (node != *list);
  133.         }
  134.         return NULL;
  135. }

  136. struct s_exp *get_exp(FILE *fp, long *offset)
  137. {
  138.         long i;
  139.         struct s_exp exp, *pexp;
  140.         int len;

  141.         i = *offset;
  142.         if (get_space(fp, &i) == 0) {
  143.                 len = get_name_length(fp, i);
  144.                 if (len > 0) {
  145.                         memset(&exp, 0, sizeof(exp));
  146.                         exp.name = (char *)malloc(len + 1);
  147.                         if (exp.name != NULL) {
  148.                                 if (get_name(fp, &i, exp.name, len + 1) == 0) {
  149.                                         if (get_text(fp, &i, "=") == 0) {
  150.                                                 if (get_space(fp, &i) == 0) {
  151.                                                         if (get_long(fp, &i, &exp.value) == 0) {
  152.                                                                 if (get_space(fp, &i) == 0) {
  153.                                                                         if (get_text(fp, &i, ",") == 0
  154.                                                                                 || get_return(fp, &i) == 0) {
  155.                                                                                 pexp = (struct s_exp *)malloc(sizeof(*pexp));
  156.                                                                                 if (pexp != NULL) {
  157.                                                                                         *pexp = exp;
  158.                                                                                         *offset = i;
  159.                                                                                         return pexp;
  160.                                                                                 }
  161.                                                                         }
  162.                                                                 }
  163.                                                         }
  164.                                                 }
  165.                                         }
  166.                                 }
  167.                                 free(exp.name);
  168.                         }
  169.                 }
  170.         }
  171.         return NULL;
  172. }

  173. int insert_exp(struct s_exp **list, struct s_exp *node)
  174. {
  175.         if (search_exp(list, node->name) == NULL) {
  176.                 if (*list == NULL) {
  177.                         node->previous = node;
  178.                         node->next = node;
  179.                         *list = node;
  180.                 }else {
  181.                         node->previous = (*list)->previous;
  182.                         node->next = *list;
  183.                         (*list)->previous->next = node;
  184.                         (*list)->previous = node;
  185.                 }
  186.                 return 0;
  187.         }else {
  188.                 free(node->name);
  189.                 free(node);
  190.                 return 0;
  191.         }
  192. }

  193. int update_exp_value(struct s_exp **list, const char *name, long value)
  194. {
  195.         struct s_exp *node;

  196.         node = search_exp(list, name);
  197.         if (node != NULL) {
  198.                 node->value = value;
  199.                 return 0;
  200.         }
  201.         return -1;
  202. }

  203. int free_all_exp(struct s_exp **list)
  204. {
  205.         struct s_exp *node, *n2;

  206.         if (*list != NULL) {
  207.                 node = *list;
  208.                 do {
  209.                         n2 = node;
  210.                         node = node->next;
  211.                         free(n2->name);
  212.                         free(n2);
  213.                 }while (node != *list);
  214.                 *list = NULL;
  215.                 return 0;
  216.         }
  217.         return -1;
  218. }

  219. int write_exp_to_file(struct s_exp **list, const char *fname)
  220. {
  221.         struct s_exp *node;
  222.         FILE *fp;

  223.         if (*list != NULL) {
  224.                 fp = fopen(fname, "w");
  225.                 if (fp != NULL) {
  226.                         node = *list;
  227.                         do {
  228.                                 fprintf(fp, "%s=%lu\n", node->name, node->value);
  229.                                 node = node->next;
  230.                         }while (node != *list);
  231.                         fclose(fp);
  232.                         return 0;
  233.                 }
  234.         }
  235.         return -1;
  236. }

  237. int read_exp_from_file(struct s_exp **list, const char *fname)
  238. {
  239.         struct s_exp *node;
  240.         FILE *fp;
  241.         long offset;

  242.         fp = fopen(fname, "rb");
  243.         if (fp != NULL) {
  244.                 *list = NULL;
  245.                 offset = 0;
  246.                 while ((node = get_exp(fp, &offset)) != NULL) {
  247.                         insert_exp(list, node);
  248.                 }
  249.                 fclose(fp);
  250.                 return 0;
  251.         }
  252.         return -1;
  253. }

  254. int main(void)
  255. {
  256.         struct s_exp *list;

  257.         if (read_exp_from_file(&list, "a.txt") == 0) {
  258.                 update_exp_value(&list, "c", 10);
  259.                 write_exp_to_file(&list, "con");
  260.                 free_all_exp(&list);
  261.                 return 0;
  262.         }
  263.         return -1;
  264. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP