0

so this is my first time posting a question here so please bear with me. I am studying computer science for my bachelors, and I would like some help. We have created various Classes that are all part of a Roster Management System. I have all the classes set up and such, and I can figure out how to store everything in a dynamically allocated array later on, for the time being I am having a difficult time just reading the data needed from a file.

The format for the text file is as follows:

course1-name | course1-code | number-credits | professor-name
student1 first name | student1 last name|credits|gpa|mm/dd/yyyy|mm/dd/yyyy
student2 first name | student2 last name|credits|gpa|mm/dd/yyyy|mm/dd/yyyy
end_roster|
course2-name | course2-code | number-credits | professor-name
student1 first name | student1 last name|credits|gpa|mm/dd/yyyy|mm/dd/yyyy
end_roster|

As you can see, the rosters only have four data fields, course name, course code, number of credits and the professors name.

Students have 6 fields, 7 if you include the end_roster marker (the end_roster marker is literally just that, not a bool value or anything.

Anyway, I cant seem to figure out how to read this input properly. I can read it all in and tokenize it, but then I don't know how to append each "section" to its correct class. This happens to give me each "token" (not 100% clear what that is, but it seems to append each set of characters into a string). I do not know how to assign it into their proper places from this point forward though.

ifstream myroster("rosters.txt");
while( ! myroster.eof() )
{
    getline(myroster, line);
    cout << line << endl << endl;
    char c[line.length() + 1];
    strcpy (c,  line.c_str());
    char * p = strtok(c, "|");
    while (p != 0)
    {
        cout << p << endl;
        p = strtok(NULL, "|");
        counter++;
    }
}
myroster.close();

I've also tried the following;

ifstream myroster("rosters.txt");
while (!myroster.eof())
{
    getline(myroster, line, '|');
    counter++;
}

Both methods had some form of counter implementation (my first attempt). For example; if counter == 4 it'll be appended to roster, or counter == 6 and its students, but nothing worked (I realized the flaw later on)

The reason I don't just hardcode the program to implement line one for roster1 information, line 2 for student 1, line 3 for student 2, and etc is because the data needs to be edited in the program, and one of the options is to add students/remove them and add rosters/remove them, after which the file needs to be updated again, and then read in again after.

Can anyone shed some light on this?

HAL
  • 3,888
  • 3
  • 19
  • 28
  • Why are you using `strtok` in `C++`? `std::string::find` or `boost::tokenizer` is better. – HAL Dec 10 '13 at 13:55
  • I do not have access to boost, or know anything of it. strtok is what my TA suggested in her help session... – thegutuproject Dec 10 '13 at 14:15
  • http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c I hope this helps. – HAL Dec 10 '13 at 14:25
  • I ran into strtok parse, which seems very promising, but does anyone know how I can format the program to determine whether input data is roster information or Student information? – thegutuproject Dec 10 '13 at 14:36

0 Answers0