程序在文本文件中打印和汇总数字

时间:2013-08-31 20:59:46

标签: c

我想编写一个程序,打印文件中找到的所有数字,然后将它们添加起来。我有两个问题:

  1. 如何加上我打印过的数字?
  2. 为什么在output_file中我有这么多逗号: Superfluous Commas
  3. 这是我的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define CHUNK 12
    
    char *getWord(FILE *infile);
    void clean(char *dirty);
    
    char *getWord(FILE *infile)
    {
        char *word, *word2;
        int length, cursor, c;
    
        word = (char*)malloc(sizeof(char)*CHUNK);
        if(word == NULL) return NULL;
    
        length = CHUNK;
        cursor = 0;
    
        while(!isspace(c = getc(infile)) && !feof(infile))
        {
            word[cursor] = c;
            cursor++;
    
            if(cursor >= length)
            {
                length += CHUNK;
    
                word2 = (char*)realloc(word, cursor);
                if(word2 == NULL)
                {
                    free(word2);
                    return NULL;
                }
                else 
                {
                    word = word2;
                }
            }
        }
    
        word[cursor] = '\0';
        return word;
    }
    
    void clean(char *dirty)
    {
        int i = 0, j = 0; 
        char *temp;
    
        temp = strdup(dirty);
        while(i < strlen(temp))
        {
            if(isdigit(temp[i]))
            {
                dirty[j] = temp[i];
                j++;
            }
    
            i++;
        }
    
        dirty[j] = '\0';
        free(temp);
    }
    
    int main(int argc, char *argv[])
    {
    
        char *word;
        FILE *infile, *outfile;
    
        if(argc != 3)
        {
            printf("Missing argument!\n");
            exit(1);
        }
    
        infile = fopen(argv[1], "r");
        if(infile != NULL)
        {
    
            outfile = fopen(argv[2], "w");
            if(outfile == NULL)
            {
                printf("Error, cannot open the outfile!\n");
                abort();
            }
            else 
            {
                while(!feof(infile))
                {
                    word = getWord(infile);
                    if(word == NULL)
                    {
                        free(word);
                        abort();
                    }
    
                    clean(word);
    
                    fputs(word, outfile);
                    fputs(",", outfile);
                    free(word);
                }
            }
        }
        else 
        {
            printf("Error, cannot open the outfile!\n");
            abort();
        }
    
        fclose(infile);
        fclose(outfile);
        return 0;
    }
    

    infile中: enter image description here

2 个答案:

答案 0 :(得分:1)

由于这个问题,你得到了, -

fputs(",", outfile);

答案 1 :(得分:0)

它在结构上与echo unix命令相关。该计划的核心可以简化为以下几点:

  int c, need_comma = 0;

  while ((c = fgetc(infile)) != EOF) {
    if (isdigit(c)) {
      fputc(c, outfile);
      need_comma = 1;
    }
    else {
      if (need_comma == 1) {
        fputc(',', outfile);
        need_comma = 0;
      }
    }
  }

这消除了对getWordclean函数的需求。

这只是印刷部分。中间文件是CSV格式, 这是结构化的,易于解析和添加数字(和打印 结果到另一个文件)。