Reading from a file into a list
Code:
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
using namespace std;
void main(int argc, char **argv)
{
// make sure that the user has specified a filename
if(argc < 2)
{
cout << endl << "You must enter a filename" << endl << endl;
return;
}
string filename = argv[1];
ifstream fin(filename.c_str()); // define our file input stream
// make sure that the file exists
if(!fin)
{
cout << endl << "Cannot find file" << endl << endl;
return;
}
/********************************************************************
Process the text data file
********************************************************************/
cout << endl << "Reading input file: " << filename << endl << endl;
int words = 0, max = 0; // to count words and longest line
string temp;
for (int line = 1; !fin.eof(); line ++)
{ // for each line in file ...
// get the line into temp string
// create string stream tokeniser
// for each word from line ...
// increment word count for line
// update total words so far
// update longest line if latest was longer
}
Im trying to do this but i dont know how to create a stream tokeniser - i know thats what i have to do but where do i start?
Thanks for your help