zoukankan      html  css  js  c++  java
  • 31. 提取text.txt和text2.txt的文件内容,进行文本等号以后求和

     1 //提取text.txt文件内容,进行文本等号以后求和
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #define F_PRINT_ERR(e)
     5 do
     6 {
     7     if(e == NULL)
     8     {
     9       printf("open error");
    10       exit(-1);
    11     }
    12 }
    13 while(0)
    14 int main(void)
    15 {
    16 
    17     FILE* pf = fopen("G:/qtcode/text.txt","r");
    18     F_PRINT_ERR(pf);
    19 
    20     char buf[1024];
    21     char* p = buf;
    22     int value,sum = 0;
    23 
    24     while(fgets(buf,1024,pf))
    25     {
    26         printf("%s",buf);
    27 
    28         p = buf;
    29         while(*p++ != '=');//遍历到每行里的等号之后第一个位置
    30 
    31         value = atoi(p);
    32         sum += value;
    33     }
    34 
    35     printf("sum = %d
    ",sum);
    36     fclose(pf);
    37 
    38     return 0;
    39 }

    text.txt文件的内容如下:每一行只有一个等号,且没有空行。

    ——————————————————————

    a =      3
        b = 4
    c   =     90

    d=   20
    e      = 25
          f      =73
    g=89
    ——————————————————————

    text2.txt文件的内容如下:每行可能有多个等号,且会有空行。
    ——————————————————————
    a =      3    b = 4   c=       90

    d=   20
    e      = 25   f=73

    g=89

    ——————————————————————

  • 相关阅读:
    timeit模块
    python中的del
    python的默认参数
    python3中的nonlocal 与 global
    python通俗讲解闭包
    vlc 视频播放器的快捷键
    Python的重要知识点汇总3
    Python的重要知识点汇总2
    Python的重要知识点汇总1
    01玩转数据结构_08_堆和优先队列
  • 原文地址:https://www.cnblogs.com/ZhuLuoJiGongYuan/p/9481090.html
Copyright © 2011-2022 走看看