Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

c - Program to print and sum numbers in a text file

I want to write a program which print all numbers found in a file and then add them up. I have two problems:

  1. How to add up the numbers I've printed?
  2. Why in output_file do I have so many commas: Superfluous Commas

Here's my code:

#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] = '';
    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] = '';
    free(temp);
}

int main(int argc, char *argv[])
{

    char *word;
    FILE *infile, *outfile;

    if(argc != 3)
    {
        printf("Missing argument!
");
        exit(1);
    }

    infile = fopen(argv[1], "r");
    if(infile != NULL)
    {

        outfile = fopen(argv[2], "w");
        if(outfile == NULL)
        {
            printf("Error, cannot open the outfile!
");
            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!
");
        abort();
    }

    fclose(infile);
    fclose(outfile);
    return 0;
}

infile: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are getting , because of this -

fputs(",", outfile);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...