Thread: Read the next line of text

  1. #1
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166

    how to read the next line of text

    I just need a quick answer.

    I have a simple program that reads names from a txt file, but I don't know how to get to read the next name.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
        
    float average;
    
    int main()
    {
        ifstream infiledudes; 
        string name; 
        infiledudes.open( "people.txt" );
        
        infiledudes>>name;
        
        cout<<"your name is"<<name;
    }
    Last edited by dra; 05-01-2005 at 01:56 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    sorry could you show me an example?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
        while ( infiledudes>>name ) {
            cout<<"your name is"<<name;
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    To read 5 names you could do this:
    Code:
    infiledudes>>name;
    cout<<"your name is"<<name;
    
    infiledudes>>name;
    cout<<"your name is"<<name;
    
    infiledudes>>name;
    cout<<"your name is"<<name;
    
    infiledudes>>name;
    cout<<"your name is"<<name;
    
    infiledudes>>name;
    cout<<"your name is"<<name;
    Now, what if you wanted to read 10,000 names? Are you willing to write those lines 10,000 times? Whenever you find yourself repeatedly writing the same lines of code, you can employ a loop:
    Code:
    while(infiledudes>>name)
    {
          cout<<"your name is"<<name;
    }
    That while loop will read all the names in the file or until there is some kind of error reading from the file. The statement:

    infiledudes>>name

    will evaluate to false if there is any kind of error reading from the file.

    If you only want to read the first 10 names from your file, you could do this:
    Code:
    for(int count = 1; infiledudes>>name; count <= 10)
    {
          cout<<"your name is"<<name;
    }
    Last edited by 7stud; 05-01-2005 at 01:31 AM.

  6. #6
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Thanks. It works but I can't fully understand why it works. Why doesn't it just keep reading the same line over and over?

    Also, say i wanted to skip the first line of text, and go straight to the 3rd one for example, how would i do that?
    Last edited by dra; 05-01-2005 at 01:57 AM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Read in the first two lines, but don't cout<< them.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by dra
    Why doesn't it just keep reading the same line over and over?
    the short answer: ifstream reads the requested number of bytes from the file and then autmotically moves forward.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    oh. I see. thanks

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i'm not in a very helpful mood tonight, but perhaps running this might help you understand..
    Code:
    //#include <iostream.h> EDIT: dunno what the hell i was thinking
    #include <iostream>
    using namespace std;
    
    int main()
    { 
      
        char c;
     
        cout << "Type in a word.\n"
        cout << "It will grab one char at a time\n"
        
        cin >> c;
        cout << c << endl;
     
        //don't ever use this (it's just quick and easy for example
        system("PAUSE");
     
        
        while(cin >> c)
                  cout << c << endl;
     
        system("PAUSE");
        return 0;
    }
    try changing "char c" to "int c" and then input something like "100 250 457 59 1 " and see what happens.....now put a character into your input string "100 200 x 53"
    Last edited by misplaced; 05-01-2005 at 03:12 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  11. #11
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    i get it.

    when you change char c to int c then enter then a sequence of numbers, only one is read at a time, because that's what "cin >> c" is requesting.

    and when you add a character in the number sequence, because it's not an integer, it's not taken.

    am I right?
    Last edited by dra; 05-01-2005 at 03:28 AM.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    more specifically, in the integer example, when it tries to read the character, cin returned false because it did not read valid data for an integer. that does not mean that it's not still in the input buffer. you could put a cin >> characterVariable after the loop and it will read the character.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  13. #13
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    alright. thanks for help. i appreciate it.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by dra
    Thanks. It works but I can't fully understand why it works. Why doesn't it just keep reading the same line over and over?
    When you do this:

    infiledudes.open( "people.txt" );

    You create a thinga-ma-bob called infilesdudes that among other things understands what ">>" means, so you can do this:

    infilesdudes>>name;

    Your infilesdudes thinga-ma-bob also has a file pointer that moves along the file everytime you read from it, keeping track of where you are in the file. cin is also one of those thinga-ma-bobs and it understands what >> means as well, and cin also has a pointer that moves along the input every time you read from the input.
    Last edited by 7stud; 05-01-2005 at 06:16 AM.

  15. #15
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by 7stud
    If you only want to read the first 10 names from your file, you could do this:

    Code:
    for(int count = 1; infiledudes>>name; count <= 10)
    {
          cout<<"your name is"<<name;
    }
    Time for you to sit in the corner for a 5 minute timeout.
    "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. Replies: 5
    Last Post: 08-01-2007, 06:17 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM