Hello and thanks in advance for the reply,
I have a text file which is in the following format, it is called "words data.txt":
word,word,word,word,...
word,word,word,word,...
...and so on for 53 lines. Every line is simply words separated by commas.
I need each line to become a vector using a loop function so that I can access it in later code.
Therefore, In my code i need for each line:
1st word of the line (of the text file) = name of vector
each word (including the first word) = values of the vector
Here is the snipet of code pertaining to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
ifstream chronosphere2("words data.txt");
int vectorsize;
vectorsize = pCWN.size();
int countn = 0;
//Vector that will have all the stored words
vector< vector <string> > chrothes;
vector <string> gename; // vector to be placed in chrothes
while (countn < vectorsize)
{
string line;
getline(chronosphere2,line );
istringstream fullline(line);
if (!line.empty())
{
string line2;
while (line2 != "")
{
getline(fullline, line2, ',');
gename.push_back(line2);
};
};
chrothes.push_back(gename);
gename.clear();
++countn;
};
if (chrothes.empty())
{
cout << "This is empty.";
}
else
{
cout << chrothes.size();
string blah;
blah = chrothes[0][0];
cout << blah << endl;
};
|
Everytime I compile and run, this crashes.
I wanted to use a map struct, but it's the same problem as vectors:
I can't use a variable for the name, or am I wrong?
Can this somehow be done:
1 2 3 4 5 6 7
|
string nameofvector; //name of vector obtained from txt file as a string
map <string, vector <> > parentmap;
.....getline().....
Lets say, first word = word1
Lets say, words in the line = word1,word2,word3...in a vector called nameofvector
parentmap[word1] = nameofvector //note that nameofvector is a variable that changes with each line of code in the file
|
Any suggestions? I hope I was clear.