Reading and sum data from a textfile with multiple numerical columns

Dear all,

Using Turbo Pascal or C or Turbo C (well primitive), how I read several columns with numbers from a text file and give the sum of each one?

Thank you in advance (if I found anyone that have programmed in Pascal.
lebron letchevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertConsultingCommented:

Not many solutions here, you'll have to read your file line by line, split each line, load everything in a 2D array (or a linearised array)
Then browse your array column by column to perform the sums.
the following ansi c code should do the job if the text file contains only numbers.

turbo c is pretty old but should compile as well.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define MAXCOLS 4
#define MAXROWS 1000

int main () 
   FILE * fp = 0;
   char buf[1024] = { 0 };   // use buffer big enough for any text line
   double data[MAXCOLS][MAXROWS] = { 0 };   // assume 4 columns and max 1000 rows
   double sums[MAXCOLS] = { 0 };
   int nrow = 0;
   int ncol = 0;  

   fp = fopen ("file.txt", "r");
   if (fp == 0)
           return errno;
   while (fgets(buf, 1024, fp) == buf)
         int len = strlen(buf);
         char * p = buf;
         char * pend = 0;
         for (ncol = 0; ncol < MAXCOLS; ++ncol)
             pend = 0;
             data[ncol][nrow] = strtod(p, &pend);
             sums[ncol] += data[ncol][nrow];
             if (pend == 0 || *pend == '\0')   // either no end or end is end-of-line
                    break;   // no more numbers ==> next row
             p = pend++;  // one char after end of previous number
         if (++nrow >= MAXROWS)
   printf("%d rows read\n", nrow);
   for (ncol = 0; ncol < MAXCOLS; ++ncol)
        printf("  sum[%d] = %9.2f\n", ncol, sums[ncol]);
   return 0;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lebron letchevAuthor Commented:
Many thanks
lebron letchevAuthor Commented:
Many thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.