Reading a line from a txt file

This is a discussion on Reading a line from a txt file within the C++ Programming forums, part of the General Programming Boards category; I've run into kind of a wall as far as reading from files goes. I need a way to read ...

  1. #1
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34

    Reading a line from a txt file

    I've run into kind of a wall as far as reading from files goes. I need a way to read the file line by line. Is there any standard function that I can use to read the file one line at a time?

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Yep!

    istream::getline will to the trick.

    Apply it to an ifstream just like you would to cin, a la:

    Code:
    char buf[255];
    
    ifstream infile;
    
    infile.open("/home/user/file", ios::in);
    
    infile.getline(buf, 255);
    
    cout << buf << '\n';

  3. #3
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Thanks, I knew it would be something easy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 10:09 AM
  2. Reading a txt file into an array: unexpected segfault
    By pistacchio in forum C Programming
    Replies: 3
    Last Post: 05-05-2009, 04:27 PM
  3. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  4. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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