File information into array of structures

This is a discussion on File information into array of structures within the C Programming forums, part of the General Programming Boards category; Once a file is read, how would you store the information into an array of nine elements where each element ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Montreal, QC
    Posts
    6

    File information into array of structures

    Once a file is read, how would you store the information into an array of nine elements where each element is a structure named data_t.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    You are going to have to provide more information.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Montreal, QC
    Posts
    6
    I need to store information from a file that contains stats of ten different countries, and store that information in an array of ten elements, where each element is a structure named data_t.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    What have you done so far?
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Montreal, QC
    Posts
    6
    I defined a structure and called the file for reading, but I am confused about how to store the information in the fscanf portion of the code.
    Code:
    typedef struct 
    {
    int ranking;
    char name[20];
    char state[2];
    float population, percent;
    } city_t[9];

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by QCMontrealQC View Post
    I defined a structure and called the file for reading, but I am confused about how to store the information in the fscanf portion of the code.
    Code:
    typedef struct 
    {
    int ranking;
    char name[20];
    char state[2];
    float population, percent;
    } city_t[9];
    The prototype for the struct should be above main(). The array of structs should be created inside main().

    Show an example of the input file, and show your attempt to read it in, please. Code must match the data to be input, if it's to work.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by QCMontrealQC View Post
    I defined a structure and called the file for reading, but I am confused about how to store the information in the fscanf portion of the code.
    Code:
    typedef struct 
    {
    int ranking;
    char name[20];
    char state[2];
    float population, percent;
    } city_t[9];
    I should point out that city_t is a type that is defined as an array of 9 (not 10) struct. Are you sure that you want to define city_t as an array?

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Code:
    for (i = 0; i < 9; i++) {
        fscanf(fp, "%d %s %s %f %f", &city_t[i].ranking, city_t[i].name, city_t[i].state, &city_t[i].population, &city_t[i].percent);
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading the file, and filling the array of structures
    By BlackOps in forum C Programming
    Replies: 13
    Last Post: 07-12-2009, 02:03 PM
  2. Reading from file into array of structures.
    By Sinensis in forum C Programming
    Replies: 1
    Last Post: 12-02-2008, 12:22 AM
  3. Filling Array Of Structures From A File
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 11-26-2008, 08:59 AM
  4. Replies: 11
    Last Post: 05-06-2007, 11:26 PM
  5. array of structures, data from file
    By nomi in forum C Programming
    Replies: 5
    Last Post: 01-11-2004, 12:42 PM

Tags for this Thread


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