In this program, I am attempting to read in a line from a file, and tokenize the line and store the values into an array. I have created a struct called "Course" which stores information about courses that I read in from the file, and has an array storing the number of courses that depend on this particular course as a prerequisite. I am having difficulty tokenizing the line and storing it into the "dependencies" array.
The file I am trying to read in with tokenizing looks like this
2 6
7 8 9 11 15
etc...
Here is the code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
using namespace std;
typedef struct Course
{
int numDependencies;
int numPrerequisites;
string name;
//stores number of courses this course depends on
int dependencies[6];
} Course;
int main()
{
int N; //Number of courses
ifstream infile("CSCCourses.txt");
if (infile.fail()){
cout << "File not found." << endl;
//exit(0);
}
//read in number of courses
infile >> N;
//dynamically allocate array of courses
Course* courseArray = new Course[N];
string line;
//loop N times to read course names
for (int i = 0; i < N; i++){
infile >> line;
cout << line << endl;
courseArray[i].name = line;
}
//loop again to read course information
for (int j = 0; j < N; j++) {
//Course dependencies[6];
getline(infile,line);
cout << line << endl;
string::size_type lastPos = line.find_first_of(" ", 0);
string::size_type pos = line.find_first_of(" ", lastPos);
while (string::npos != pos || string::npos != lastPos) {
//found token, add to array.
dependencies[].push_back(line.substr(lastPos, pos - lastPos));
//skip delimiters
lastPos = line.find_first_not_of(" ", pos);
//find next non delimiter
pos = line.find_first_of(" ", lastPos);
}
}
infile.close();
}