MicroMAG
MicroMAG

Reputation: 1

I am trying to fill an array of size [i][j] with data from a txt file in C

Here is an example of what I have done. Where is my logic faulty? I open up the file and declare an array of some size. Yet I cant seem to fill the array.

#include <stdio.h>

int main()
{
    /*open the file from use input*/
    printf("what file stores your cities temperature data?\n");
    char filename[20];
    scanf("%s", filename);
    FILE *ifp;
    ifp=fopen(filename, "r");
    if (ifp==NULL)
    {
        printf("File Open Error...");
    }
    else
    {
        /*declare an array and fill with file data?*/
        double cityarray [6102][4];

        int i,j;
        for(i=0;i<6102;i++)
        {

            for(j=0;j<4;j++)
            {
                fscanf(ifp, "%.1lf", cityarray[i][j]);
                printf("%.1lf\t",cityarray[i][j]);
            }
            printf("\n");
        }

        fclose (ifp);

    }
    return 0;
}

Upvotes: 0

Views: 93

Answers (1)

yoprogramo
yoprogramo

Reputation: 1306

try:

fscanf(ifp, "%.1lf", &cityarray[i][j]);

Upvotes: 1

Related Questions