Reading from a file into a list

This is a discussion on Reading from a file into a list within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <strstream> #include <string> using namespace std; void main(int argc, char **argv) { // make ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Here's a hint, just look a couple posts below this one.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 05:45 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. reading in a txt file into a list
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 12-25-2007, 12:44 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Reading a file into a Linked List
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-20-2002, 07:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21