A lot of people have questions about how to load a text file using file I/O. There seems to be 2 popular methods to loading a text file:
Use fstream's >> extraction operator. Simple enough, load the file directly into individual containers, word at a time, but you lose your delimiting white spaces (assuming you need them). So some file integrity is lost, unless you make the effort re-construct the file by re-inserting all the white spaces.
So instead, you decide to use getline(). You preserve all white spaces and load the file line by line... but now you have to parse a line of data into individual substrings; either by using <string> member functions, performing string[] array operations, or using strtok().
One alternative I would like to suggest: why not do both? It is possible to read a file in it's entirety and read in text 'word at a time' into individual containers.. without having to do any string parsing:
#include<string>
//Be sure to open your file in binary mode
infile.open("C:\\Users\\Dave\\Documents\\test.txt", ifstream::binary);
//Here you can load the file in it's entirety
while(getline(infile, lines[i]))
{
//Go back to the start of the line
infile.seekg(begin, ios::beg);
//Now you can load the same data into individual containers
for(int j=0; j<4; j++)
{
infile >> words[word_count];
word_count++;
}
//Discard any extra characters left behind
infile.ignore(100, '\n');
//Save current position, so you can go back to the beginning of next line
begin = infile.tellg();
i++;
}
So now you have the entire document preserved in lines[], and you have individual line contents stored in words[]. Depending on your application needs, this method might be a viable option in that you get the best of both worlds without having to do any string parsing.