Title: Tokenize a string in c++ without boost
Tags: c++;arrays;struct;tokenize
Question: 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&gt;
#include <fstream&gt;
#include <cstdlib&gt;
#include <string&gt;
#include <cstdio&gt;
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 &gt;&gt; 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 &gt;&gt; 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;
string75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_type lastPos = line.find_first_of(" ", 0);
string75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_type pos = line.find_first_of(" ", lastPos);
while (string75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0npos != pos || string75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0npos != 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();
Comment: Look here for some ideas: [How do I tokenize a string in C++?](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c?rq=1)

Comment: Check [`st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0istringstream`](http://en.cppreference.com/w/cpp/io/basic_istringstream)

Comment: Thanks! These are very helpful.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:02/15/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><arrays><struct><tokenize>', 'Title': 'Tokenize a string in c++ without boost', 'ViewCount': 17, 'id': 28530256, 'user_AboutMe': None, 'user_AccountId': 5055955.0, 'user_DisplayName': 'Geoffrey Weber', 'user_Reputation': 25.0, 'user_Views': 18.0, 'user_id': 4059190.0}}