easy Q -> read a whole line from file

This is a discussion on easy Q -> read a whole line from file within the C++ Programming forums, part of the General Programming Boards category; i've done a bit of searching ubt havnt found anything its quite simple.. i want to read a line from ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    easy Q -> read a whole line from file

    i've done a bit of searching ubt havnt found anything
    its quite simple..
    i want to read a line from a file.
    since the default
    ifstream blah("blah.txt");
    blah >> word;
    gets a word at a time

    how would i read in a whole line from a file stopping at a \n

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Have you heard of the almighty getline?
    http://www.cplusplus.com/ref/iostrea...m/getline.html
    Woop?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    11
    Someone correct me if I'm wrong but following your example( paperbox005 ) I believe it would be:
    Code:
     ifstream blah("blah.txt");
     getline( blah, word );

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Only if 'word' is a std::string, which hopefully it is since they're so much nicer than C-style strings.

    **EDIT**
    Alternately, if word is a char[50] or something:
    blah.getline(word, 50);
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Very strange error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 01:37 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 07:21 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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