VictorS
VictorS

Reputation: 17

reading two arrays from a file

I am new to c programming and I am having a problem with one of my programs. I am supposed to read three arrays from a file. the file I am using is Temps.txt below is what is in the file.

1           31          37
2           26          24
3           30          38
4           33          25

5           33          21
6           29          28
7           41          46

this goes down all the way till left column is 31. The code I have written is

#include<stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    FILE *readfile;
    int New_York[31];
    int Anchorage[31];
    int Dates[31];
    int i;
    printf( "Date    New York    Anchorage\n" );
    if( ( readfile = fopen( "Temps.txt", "r" ) ) == NULL )
    {
        printf( "The file Temps failed to open\n" );
    }
    else
    {
        for( i = 0; i < 31; i++ )
        {
            fscanf( readfile, "%d, %d, %d", Dates + i, New_York + i, Anchorage + i );
            printf( "%d     %d     %d\n", *(Dates + i), *(New_York + i), *( Anchorage + i ) );
        }
        if( fclose(readfile) == EOF ) //close the file.
        {
            printf("The file failed to close.\n");
        }
    }

    return(0);
}

When i compile it it runs and all of the data that it is reading is printed in the Dates array, in the other two arrays i am getting really large numbers negative and positive. If you could please help I would really appreciate it.

Thank you

Upvotes: 0

Views: 376

Answers (1)

Comet
Comet

Reputation: 288

Get rid of the commas: "%d, %d, %d"

fscanf( readfile, "%d %d %d", Dates + i, New_York + i, Anchorage + i );

Upvotes: 4

Related Questions