Thread: Problems reading a data file into an array of structures

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    1

    Problems reading a data file into an array of structures

    I'm reading the following file:
    Osgood,Marcus 298542123 CHM FR [email protected]
    Cronk,Melissa 873489021 BIO SR [email protected]
    Pry,Seth 349908431 MTH SO [email protected]
    Langlais,Susan 783323545 ME SR [email protected]
    Davis,Nicole 987543345 PHY FR [email protected]


    It's supposed to split it up into name, ID number, major, year, and email. The file reads it without any errors, and assigns name to the first part of the structure. However, ID gets assigned the ID, major, year, and email. Then Major gets assigned major, year, and email. Year gets assigned year and email, while email just gets assigned email. I don't know if it has something to do with the loop. For example, this is what I get what I print just the name and the ID.


    Cronk,Melissa [email protected]


    Pry,Seth [email protected]


    Langlais,Susan 783323545ME


    Davis,Nicole 987543345 [email protected]


    -------------------------------------
    Anyway. This is my function code for reading the array. I have it printing the ID number just to see if I can catch the errors earlier:

    Code:
        #include <stdio.h>
        #include <string.h>
        #include "functions.h"
    
    
        void read_data(char filename[], studentinfo_t s[], int *size)
        {
            FILE *infilep;
            int countstudents = 0;
            infilep = fopen("student_data", "r");
    
    
    
    
        while (fscanf(infilep, "%s  %s  %s  %s  %s", s[countstudents].name, s[countstudents].idnum, s[countstudents].major, s[countstudents].year, s[countstudents].email) != EOF)
        {
              countstudents++;
        }
        *size = countstudents;
        printf("\n%s\n", s[countstudents].idnum);
        fclose(infilep);
    
    
        }



    I do not know what I am doing here, the name is reading correctly, but not anything else!
    Help would be greatly appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    How did you declare studentinfo_t ?

    Because if you only allocated 3 chars for say "CHM" or "BIO", then you're going to see all sorts of weird stuff happening.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from file into array of structures.
    By Sinensis in forum C Programming
    Replies: 1
    Last Post: 12-02-2008, 01:22 AM
  2. Reading data file problems!
    By noodle24 in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 11:13 AM
  3. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  4. array of structures, data from file
    By nomi in forum C Programming
    Replies: 5
    Last Post: 01-11-2004, 01:42 PM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM

Tags for this Thread