Thread: How do I read a line from a file?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    How do I read a line from a file?

    I'm new to C++ and I'm teaching myself with the tutorial examples on the website. However I was wondering how do you read a full line from a text file? When it is input into a string it only reads until the first space.

    Best regards

    Ploe.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    ifstream in ("whatever.txt");
    // leaving out error checking
    
    string MyString;
    getline( in, MyString );
    
    cout<< MyString;
    Did you look at the FAQ, or search?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    No sorry, like I said I'm new. But thank you very much that's exactly the information I wanted. From now on I'll look at the FAQ or search first.

    Thanks again though!

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> [...] the FAQ or search first.

    You, of course mean, FAQ and search, right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM
  4. how can i read i line from a file
    By condorx in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 02:47 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM