C Programming help. File I/O, dat file, into and integer array for calculations?

Okay, I really don't know what else to try. I'm using Codeblocks on Windows 7 to run C...

I have been given a problem where I need to read from a file 3 numbers from a line, there are 12 lines. I should then sum all values in the file.

Basically, the files is "addition.dat".

The info is in the format:

12.60 09.32 12.34

39.21 41.60 00.64

3 float numbers separated by a space.

I have tried many different things, but don't seem to be able to do it.

The closest I got was

#include <stdio.h>

#include <stdlib.h>

#define MAXROWS 12

#define MAXCOLS 20

//const char

FILE *file_pointer;

int main(){

int i=0;

int j=0;

char ch;

int array[MAXROWS];//[MAXCOLS];

char *arr;

arr = &array;

file_pointer = fopen("addition.dat","r");

while((ch = fgetc(file_pointer)) != EOF)

*(arr++) = ch;

fclose(file_pointer);

return 0;

}

If I put in:

printf("%s", array);

at the end, it prints everything in the file... But I can't then use the information.

Please help, will vote best answer.

A good range of answers would be good, maybe adapt mine, or one from scratch, or both.

Thank you

2 Answers

Relevance
  • cja
    Lv 7
    9 years ago

    If all you care about is the sum of the numbers in the file, you don't have to store them, or limit the amount of numbers you'll accept. If the numbers may not be integers, you need to read them as float or double. An intermediate step of reading the file as text into your program is not necessary. Also, if you're going to hardcode the file name, you might as well take input from stdin - your program will be more flexible that way. Here's a good way to do it:

    /*

      * sum

      *

      * Accepts a set of numbers from standard input and computes the sum.

      *

      * Input may be redirected from a file, received through a pipe, or entered

      * interactively. If used interactively, input must be terminated with

      * ctrl-d after the final carriage return.

      *

      * Any number of numbers may be entered per line. Numbers may be separated

      * by any number and combination of spaces, commas, or tabs. Leading blanks

      * and blank lines are OK.

      *

      * Usage: sum

      *

      */

    #include <stdio.h>

    #include <string.h>

    #define NUMSTR_LEN 1024

    #define WHITESPACE " ,\t"

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

        char numstr[NUMSTR_LEN], /* entered line */

                  *s; /* pointer to next number in line */

        double n, /* value of entered number */

                      total=0.0; /* sum of entered numbers

        /*

          * Read each number on each line entered. Add each value to the running total.

          */

        while ((s = fgets(numstr,NUMSTR_LEN,stdin)) != NULL) {

            while (s != (char *)NULL) {

                if (sscanf(s,"%lf",&n) == 1) {

                    total += n;

                }

                if ((s = strpbrk(s,WHITESPACE)) != (char *)NULL) {

                    s = &s[strspn(s,WHITESPACE)];

                }

            }

        }

        printf("%g\n", total);

        return 0;

    }

    Sample run:

    $ cat addition.dat | ./sum

    115.71

    Where:

    $ cat addition.dat

    12.60 09.32 12.34

    39.21 41.60 00.64

  • roger
    Lv 7
    9 years ago

    you are reading the file character by character

    you want to read in the data as floating point numbers

    you can use fscanf() for this

    http://www.cplusplus.com/reference/clibrary/cstdio...

    float num,sum=0;

    file_pointer = fopen("addition.dat","r");

    while( fscanf(file_pointer," %f",&num)!=0){ // note the space in front of the % sign -- that tells fscanf() to ignore white space before the number

    sum+=num;

    }

    printf("the sum is %f\n",sum);

    fclose(file_pointer);

    return 0;

    }

Still have questions? Get your answers by asking now.