Filling Array Of Structures From A File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • triphoppa
    New Member
    • Oct 2008
    • 16

    #1

    Filling Array Of Structures From A File

    I'm having a hard time discovering how to fill an array of structures from a file. This is what I'm trying to do.

    I am going to open a file from the command line. In the file are a bunch of records some of them in the proper format and some not. Each of the records are on one line of the file, like this.

    Sumpter, David: 12445: 100: 85: 70: 78: B+
    Laporte, Leo: 23855: 88:88: 100: 76: B
    .
    .
    .
    ...and so on

    Any ideas how I can get this type of data into an array of structures dynamically?

    I don't need to handle all the records in the file just the first 100 valid records.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I assume your struct will look something like:

    Code:
    struct Student {
       string lname, fname, letterGrade;
       int idNum, grades[4];
    };
    (making the assumption based on your sample data that you are talking about students)

    Can you assume that all the entries in your data file are separated by a ':'? If so, you can

    a) Get a line from the file
    b) Look for the first ':', and use everything before it as the last, first name.
    c) Look for the next ':', and use everything before that as the idNum
    d) Look for the next ':', and use everything before it as a grade
    e) Repeat (d) for each entry in grades
    f) Finally, take the remainder of the string as letterGrade.
    g) Repeat (a)-(f) 100 times

    This approach assumes that, as you get data from the line, you destroy the line in the process (i.e. after step (b), a data line might look like " 12445: 100: 85: 70: 78: B+").

    However, if you cannot make the assumption that all your data entries will be separated by a ':', your job becomes almost impossible, and you may want to consider invalidating certain lines before even trying to process them.

    Comment

    • triphoppa
      New Member
      • Oct 2008
      • 16

      #3
      Thanks that's just the jump start I needed!!

      Comment

      Working...