Sumowanie liczb z pliku tekstowego.

0

Chcialem napisac program, ktory primo wyswietla liczby z pliku i secondo sumuje je. Z wyswitlaniem chyba sobie poradzilem ale nie mam pomyslu na dodawanie. Wysylam kod programu oraz plik wejsciowy:

#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, coma = 0;

    word = (char*)malloc(sizeof(char)*CHUNK);
    if(word == NULL) return NULL;

    length = CHUNK;
    cursor = 0;

    while((c = getc(infile)) != EOF)
    {
        if(isdigit(c))
        {
        word[cursor] = c;
        coma = 1;
        cursor++;

            if(cursor >= length)
            {
                length += CHUNK;

                word2 = (char*)realloc(word, cursor);
                if(word2 == NULL)
                {
                    free(word2);
                    return NULL;
                }
                else 
                {
                    word = word2;
                }
            }
        }
        else
        {
            if(coma == 1)
            {
                word[cursor] = ',';
                coma = 0;
                cursor++;
            }
        }
    }

    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;
} 

Za jakakolwiek pomoc bede wdzieczny.

0

Czyli coś takiego:

#include <stdio.h>

int main(int argc,char *argv[])
  {
   FILE *fin;
   int sum=0,val;

   fin=fopen(argc>1?argv[1]:"con","r");
   if(!fin)
     {
      fprintf(stderr,"Error, can't open file %s\n",argv[1]);
      return 1;
     }
   while(!feof(fin))
     {
      if(fscanf(fin,"%d",&val)==1) printf("+ %d = %d\n",val,sum+=val);
      else getc(fin);
     }
   fclose(fin);
   return 0;
  }

próbowałeś napisać?

0

okay, juz chyba mam tylko jezeli operuje na stringach to jak je moge dodawac?

0

Okay, dziala prawie wszystko, poza jednym. Dlaczego w pliku wyjściowym liczby mam oddzielone zerami? W załączniku przesalam plik wejsciowy i wyjsciowy. Moj poprawiony kod:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define CHUNK 12

char *getNumber(FILE *infile);

int main(int argc, char *argv[])
{
    char *number, *pEnd;
    FILE *infile, *outfile;
    int newNumber, sum = 0;

    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))
            {
                number = getNumber(infile);

                if(number == NULL)
                {
                    free(number);
                    abort();
                }
                newNumber = strtol(number, &pEnd, 10);
                sum += newNumber;

                 if(!*pEnd)
                    printf("Converted successfully!\n");
                    else printf("Conversion error, non-convertible part: %s", pEnd);

                fprintf(outfile, "%d\n", newNumber);
                free(number);
            }
            fprintf(outfile, "\nSum: %d\n", sum);
        }
    }
    else
    {
        printf("Error, cannot open the infile!\n");
        abort();
    }

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

char *getNumber(FILE *infile)
{
    char *number, *number2;
    int length, cursor = 0, c;

    number = (char*)malloc(sizeof(char)*CHUNK);
    if(number == NULL)
    {
        printf("Error!\n");
        return NULL;
    }

    length = CHUNK;

    while(!isspace(c = getc(infile)) && !feof(infile))
    {
        if(isdigit(c))
        {
            number[cursor] = c;
            cursor++;

            if(cursor >= length)
            {
                length += CHUNK;
                number2 = (char*)realloc(number, cursor);
                if(number2 == NULL)
                {
                    free(number);
                    return NULL;
                }
                else number = number2;
            }
        }
    }
    number[cursor] = '\0';
    return number;
}

Prosze o jakakolwiek wskazowke dlaczego tak jest?

0

Każdy string, który nie jest liczbą traktowany jest jako 0.

0

Problem rozwiazany, przesylam dzialajacy kod:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#define CHUNK 12

char *getNumber(FILE *infile);

int main(int argc, char *argv[])
{
    char *number, *pEnd;
    FILE *infile, *outfile;
    int newNumber, sum = 0;

    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))
            {
                number = getNumber(infile);

                if(number == NULL)
                {
                    free(number);
                    abort();
                }
                errno = 0;
                newNumber = strtol(number, &pEnd, 10);
                sum += newNumber;

                if ((errno == ERANGE && (newNumber == LONG_MAX || newNumber == LONG_MIN)) || (errno != 0 && newNumber == 0)) 
                {
                    exit(EXIT_FAILURE);
                }

                if(*pEnd != '\0')
                {
                    printf("Further character after number: %s\n", pEnd);
                    exit(EXIT_FAILURE);
                }

                if(0 != newNumber) 
                fprintf(outfile, "%d\n", newNumber);
                free(number);
            }
            fprintf(outfile, "\nSum: %d", sum);
        }
    }
    else
    {
        printf("Error, cannot open the infile!\n");
        abort();
    }

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

char *getNumber(FILE *infile)
{
    char *number, *number2;
    int length, cursor = 0, c;

    number = (char*)malloc(sizeof(char)*CHUNK);
    if(number == NULL)
    {
        printf("Error!\n");
        return NULL;
    }

    length = CHUNK;

    while(!isspace(c = getc(infile)) && !feof(infile))
    {
        if(isdigit(c))
        {
            number[cursor] = c;
            cursor++;

            if(cursor >= length)
            {
                length += CHUNK;
                number2 = (char*)realloc(number, cursor);
                if(number2 == NULL)
                {
                    free(number);
                    return NULL;
                }
                else number = number2;
            }
        }
        else if (cursor != 0)
        {
            break;
        }
    }
    number[cursor] = '\0';
    return number;
}
0
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

void assertion(int err,int code,const char *msg)
  {
   if(!err) return;
   fprintf(stderr,"%s\n",msg);
   exit(code);
  }

int main(int argc,char *argv[])
  {
   FILE *infile,*outfile;
   int ch,inside=0,val=0,sum=0; 
   assertion(argc!=3,1,"Missing argument!");
   infile=fopen(argv[1],"r");
   assertion(infile==NULL,2,"Error, cannot open the infile!");
   outfile=fopen(argv[2],"w");
   assertion(outfile==NULL,3,"Error, cannot open the outfile!");
   while(!feof(infile))
     {
      ch=getc(infile);
      if(isdigit(ch))
        {
         inside=1;
         val*=10;
     val+=(ch-'0');
        }
      else if(inside)
        {
         inside=0;
         fprintf(outfile,"%d\n",val);
         sum+=val;
         val=0;
        }
     }
   fprintf(outfile,"\nSum: %d\n",sum);
   fclose(outfile);
   fclose(infile);
   return 0;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0