Thread: Reading from file into array of structures.

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Reading from file into array of structures.

    I'm trying to read a student record into an array (sAry) of STUDENT structures, from a text file.

    The students name is a string, max 20 characters.
    Student ID is a six digit integer.
    Four, three digit, integer score fields.
    A grade which is a string, max 2 characters.

    All fields are separated by a semicolon, example:
    Smart, Maxwell:868686:55:64:99:99:C+

    This is the code I'm trying to use:
    Code:
    	while (i < MAX_STUDENTS)
    	{
    		fgets(buffer, 500, FILE);
    		if (sscanf(buffer, "%20[^:]%*[:]%d%*[:]%d%*[:]%d%*[:]%d%*[:]%d%2[^:]",
    			sAry[i].name, &sAry[i].id, &sAry[i].score1, 
    			&sAry[i].score2, &sAry[i].score3, &sAry[i].score4, sAry[i].grade) > 0)
    			{
    				i++;
    			}
    	}
    I'm getting a segmentation fault. I've tried playing around with it and nothing. Can anyone point out the flaw I must be missing?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    *hits self with hammer in the head*

    ...I forgot ampersands..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filling Array Of Structures From A File
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 11-26-2008, 09:59 AM
  2. reading a file into an array
    By dantegl36 in forum C Programming
    Replies: 11
    Last Post: 12-02-2006, 12:23 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. reading from file to an array - help needed!
    By timeforheroes in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 12:16 PM