Trouble with reading in data to an array of structs

This is a discussion on Trouble with reading in data to an array of structs within the C Programming forums, part of the General Programming Boards category; I've broken down the huge program I was recently assigned into several parts, and this is the first. I'm trying ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    25

    Trouble with reading in data to an array of structs

    I've broken down the huge program I was recently assigned into several parts, and this is the first. I'm trying to read in data from an input file and store each piece into structs in an array. Here is the code I developed:

    Code:
        //Reads in the input data for the number of players.
        fscanf(ifp, "%d", total_players);
    
        KBLottoPlayer test[total_players];
    
        //Reads in the other pieces of information from the input file and
       //prints to confirm the data was read in successfully.
        for(i = 0; i < total_players; i++)
        {
            fscanf(ifp, "%s%s", test[i].lastname, test[i].firstname);
            printf("%s %s \n", test[i].lastname, test[i].firstname);
    
            for(j = i; j < total_players; j++)
            {
                fscanf(ifp, "%d", test[i].numbers[j]);
                printf("%d", test[i].numbers[j]);
    
                if(j == 5)
                    printf("\n");
            }
        }
    I've tried every solution I could think of, but unfortunately, the program keeps crashing. What am I doing wrong? (P.S. Everything with the struct and file is declared correctly, and I declared the struct array as I was shown in the book.)

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,478
    Post how KBLottoPlayer is declared.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    Code:
    typedef struct 
    {
        char firstname[20];
        char lastname[20];
        int numbers[6];
    } KBLottoPlayer;
    I also tried to declare without typedef (struct KBLottoPlayer), and it still crashes.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,478
    Code:
    for(j = i; j < total_players; j++)
    Why do you think value of "j" will be valid in ".numbers[j]"?
    In what way is the value of j related to i or total_players?
    Note: There is no way there are related.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    Even after fixing it or removing the code, the program STILL crashes. The only thing I didn't try doing is freeing up memory for the array of structs, but A. that will be done in another test file I plan to make, and B. I should be able to perform this task without it.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    Oh, and here's what the input file looks like:

    5
    Llewellyn Mark
    1 15 19 26 33 46
    Young Brian
    17 19 33 34 46 47
    Cazalas Jonathan
    1 4 9 16 25 36
    Siu Max
    17 19 34 46 47 48
    Balci Murat
    5 10 17 19 34 47

    I just had an epiphany. Like others, my program is designed to read things all on one line. However, the strings and integers are on separate lines. Could that be what causes the crash, or should it be working with the code I posted?
    Last edited by ljgerr93; 01-22-2012 at 09:37 AM.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    Welp, I've confirmed what I said above. I'm back in business until further notice!
    Last edited by ljgerr93; 01-22-2012 at 01:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with reading data with istringstream
    By CppProgrammer33 in forum C++ Programming
    Replies: 9
    Last Post: 08-20-2010, 11:11 AM
  2. Trouble Reading Data from Files
    By CConfusion in forum C Programming
    Replies: 11
    Last Post: 04-06-2006, 07:12 PM
  3. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  4. trouble reading data from a file
    By bob56 in forum C Programming
    Replies: 14
    Last Post: 03-17-2005, 08:03 AM
  5. having trouble reading data from a file
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2001, 10:15 AM

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