Search code examples
c++xcodestructunionscstring

Reading in arbitrary numbers of a particular word into a cstring


I am trying to read a text file into a cstring array with struct/union implementations. First off, this is what my text file looks like:

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

Now, ignoring the obvious Asian popstar references my lecturer decided to fill this example with - I'd like to be able to read the first number before the CSCI, which is the number or courses and their respective grades and automate(loop) it for all the 3 (or possibly more) students in the text file.

I have not written any code for what I'm trying to describe here but here is what I have so far.

#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;
}

This is what my final text file with all the array information processed, would look like:

https://i.sstatic.net/4g4CM.jpg

Any help is appreciated, thanks.


Solution

  • You can read in each course list this way:

    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`...
    }