Чтение произвольного числа конкретного слова в строку

Я пытаюсь прочитать текстовый файл в массив cstring с реализациями struct / union. Во-первых, вот как выглядит мой текстовый файл:

F South Korea
Male Psy Park Jae Sang
31 - 12 - 1977
3 CSCI114 55 CSCI103 44 GangNam 100

S Female Super Junior
5 - 8 - 1978
2 CSCI114 60 CSCI103 80

F People Republic Of China
Unknown James Bond
11 - 12 - 1976
4 CSCI114 54 CSCI124 66 CSCI007 99 CSCI123 28

Теперь, игнорируя очевидные ссылки азиатских поп-звезд, мой лектор решил заполнить этот пример — я хотел бы иметь возможность прочитать первый номер перед CSCI, который является номером или курсами и их соответствующими оценками, и автоматизировать (цикл) его для все 3 (или, возможно, больше) студентов в текстовом файле.

Я не написал никакого кода для того, что я пытаюсь описать здесь, но вот что у меня есть до сих пор.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAX = 80;

struct Birthday
{
char day[MAX];
char month[MAX];
char year[MAX];
};

struct Local
{
char name[MAX];
char gender[MAX];
Birthday bd;
char subjects [MAX];
};

struct Foreigner
{
char name[MAX];
char nationality[MAX];
char gender[MAX];
Birthday bd;
char subjects [MAX];
};

union Student
{
Local     localStudent;
Foreigner foreignStudent;
};

enum CountryType {S, F};

struct UowStudents
{
CountryType ct;
Student st;
};

int fileToArray (fstream& afile, char fileName [], UowStudents& x);

int main ()
{
srand(time_t(NULL));

fstream afile;
UowStudents x [MAX];
char fileName[MAX];
cout << "Enter filename: ";
cin >> fileName;
int size = fileToArray (afile, fileName, *x);}

int fileToArray (fstream& afile, char fileName [], UowStudents& x)
{
afile.open(fileName, ios::in);

if (!afile)
{
cout << fileName << "could not be opened for read" << endl;
exit (-1);
}
//file to array.
char locale;
char dateJunk;
afile >> locale;
afile.getline(x.st.foreignStudent.nationality, MAX);
afile >> x.st.foreignStudent.gender;
afile.getline(x.st.foreignStudent.name, MAX);
afile >> x.st.foreignStudent.bd.day;
afile >> dateJunk;
afile >> x.st.foreignStudent.bd.month;
afile >> dateJunk;
afile >> x.st.foreignStudent.bd.year;//Tests my cstring arrays to see everything is copied in correctly.
cout << locale << " " << x.st.foreignStudent.nationality;
cout << endl;
cout << x.st.foreignStudent.gender;
cout << x.st.foreignStudent.name;
cout << endl;
cout << x.st.foreignStudent.bd.day << " - ";
cout << x.st.foreignStudent.bd.month << " - ";
cout << x.st.foreignStudent.bd.year;
return 0;
}

Вот как будет выглядеть мой окончательный текстовый файл со всей обработанной информацией массива:

http://i.imgur.com/HSLvp.jpg

Любая помощь приветствуется, спасибо.

0

Решение

Вы можете прочитать в каждом списке курсов следующим образом:

int numOfCourses;
afile >> numOfCourses;
for (int i = 0; i < numOfCourses; i++)
{
string course;
afile >> course;

int grades;
afile >> grades;

// Fill in structure from `course` and `grades`...
}
0

Другие решения

Других решений пока нет …