Reading from a file into an array

This is a discussion on Reading from a file into an array within the C Programming forums, part of the General Programming Boards category; Hello to all. I am trying to read from a file, weather.txt, into an array (using functions), and print that ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Reading from a file into an array

    Hello to all. I am trying to read from a file, weather.txt, into an array (using functions), and print that array once created. I believe I have before and after main done correctly, but the content of main I am lost on. I have never been good at functions, so that's why I am trying this, to learn more.

    What I have so far:

    Code:
    #include <stdio.h>
    
    
    typedef struct{
    
    int avg_high;
    int avg_low;
    int avg_wind;
    float avg_precip;
    
    } weather_t;
    
    const int nbr_months = 12; 
    const char weather_file [] = "weather.txt";
    
    FILE * fptr;
    
    int loadstats(FILE * wfile, weather_t wstats[]);
    void printstats (weather_t wstats[]);
    
    
    
    int main(void)
    {
    
    	fptr=fopen("wfile", "r");
    
    	//I have opened the file, but how do I place it into the array?
    
    
        return 0;
    }
    
    int loadstats(FILE * wfile, weather_t wstats[])
    {
    int k;
    
    for(k = 0; k < nbr_months; k++)
    
    	fscanf(fptr, "%i%i%i%f", &wstats[k]);
    	printf("%i%i%i%f"), wstats[k];
    
    
    }
    This runs with no error, but does nothing (which makes sense, as there is no code in main). I just don't understand how to read and place into an array. A C reference book I looked at said to use:

    Code:
    int = fscanf(FILE * , formatstring, list of destination(addresses));
    to read from a file into an array. So I think for formatring I would place
    Code:
     %i%i%i%f
    , because what I wish to read contains three ints and a float, but I am not sure. As well, I don't understand what this piece of code means by destination(addresses), or if it is right at all to use such code. Thank you all for any help you can give!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Destination is a perfectly ordinary English word, meaning "where you want [the data, in this case] to go". In this case, you have four data things, so you need four destinations for the data. Note: You're going to need way more than one array.

    If you're not good at functions, how have you been using printf and scanf all this time? Edit: Or for that matter, main()?
    Last edited by tabstop; 10-18-2008 at 04:47 PM.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    I have made functions before, back when I was in highschool, in Java and C. but like I said, am not very good at them, and am trying to learn. Destinations, so the destination I wish them to be in is the array declared before main, in the first four positions. I tried:

    Code:
    int a,b,c,d;
    
    a=0;
    b=1;
    c=2;
    d=3;
    
    ......
    
    int = fscanf(FILE * , (%i%i%i%f), wstats([a],[b],[c],[d]);

    To no avail. it returns that line of code is an "Empty Declaration." So the destination is not the places in the array I wish the data to be stored? Thanks again.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    FILE * is a type, not an actual file pointer.
    I don't know how you got from "%i%i%i%f" to (%i%i%i%f).
    wstats([a],[b],[c],[d]) should be a syntax error: attempting to call a function called wstats with four parameters.

    Anyway, you can't have mixed types in an array -- you will not be able to put this all into one array, unless you make a structure for each day's statistics.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    Alright, changed them all to ints:

    Code:
    int avg_high;
    int avg_low;
    int avg_wind;
    int avg_precip;
    
    ...
    
    
    
    int = fscanf(FILE * , "%i%i%i%f", wstats(.......);
    
    //Anything I place inside the parenthesis,
    // ([a][b][c]), or (a,b,c), (0,1,2), or leaving it //blank, always returns "Empty Declaration."
    It still returns empty declaration, whatever I put in the parenthesis, am I completely doing this wrong? Thanks again.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you have a function called wstats? No? Then why are you trying to call it?

    The empty declaration, by the way is because you're trying to declare a variable of type int and name "", whose value is the result of the function fscanf.

    I'm going to burn for this, but:
    Code:
    fscanf(wfile, "%i%i%i%f", &wstats[i].avg_high, &wstats[i].avg_low, &wstats[i].avg_wind, &wstats[i].avg_precip);
    Whether or not you're good at functions, you've been programming long enough to know that types don't go in function calls, nor do you just sprinkle them in random places but only include them when you're trying to declare a new variable.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    21
    Ah, I had:

    Code:
    fscanf(fptr,"%s %i %i %i %f \n", &wstats[i].avg_high, &wstats[i].avg_low, &wstats[i].avg_wind, &wstats[i].avg_precip);
    Outside of main a couple days ago. That didn't run so I thought it was wrong and I started fresh. Thank you for your time and help, and sorry if I angered you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 12:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21