Thread: Reading text file

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    Unhappy Reading text file

    I need to display 24 lines of text from a file, then ask user to press enter and display 24 for lines, etc, etc, til end of file, but have no clue. My book doesn't give any advice on how to perform the operation.....Here's my code. Thanks for any clues.

    Code:
    #include <fstream>
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {
        
        const int SIZE = 5000;
        char fileName[SIZE];    
        fstream file;
        int count = 0;
        
        
        cout << "Enter the file name:";
        cin >> fileName;
        
        file.open(fileName, ios::in);
        file.getline(fileName,SIZE);  
        
        
       
        
        while (!file.eof())
        
            {
            count++;              
            cout<<count<<": ";            
            cout << fileName << endl;       
            file.getline(fileName, SIZE);
           
              }  
              
              file.close();           
        
        system("PAUSE");
        return 0;
    }

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    1. getline terminates at newline, unless otherwise specified.
    2. Don't check for eof().... instead check for the valdity of the read function.
    3. Why were you counting? Look at your code.... what would a computer do?

    Code:
    int main()
    {
        //Please enter file name
        //ifstream object
        for(24 times)
        {
             getline(ifstream_object, stringbuf);
             cout << stringbuf << endl;
        }
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM