Thread: getline by line

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Considering you want to read the first 100 lines, you need to store each line in a variable. A vector would be the best solution.

    So start by defining a vector and a string (line) so you can store each line in each vector's position.

    If you wanted just a couple of lines, you'd be better off repeating the code you posted previously plus pushing the string into the vector, but considering you're willing to do it 100 times, just create a loop from 0 to 99/100, get each line of the file (getline) and push it back to the vector (myVector.push_back).

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Quote Originally Posted by Khabz View Post
    Considering you want to read the first 100 lines, you need to store each line in a variable. A vector would be the best solution.

    So start by defining a vector and a string (line) so you can store each line in each vector's position.

    If you wanted just a couple of lines, you'd be better off repeating the code you posted previously plus pushing the string into the vector, but considering you're willing to do it 100 times, just create a loop from 0 to 99/100, get each line of the file (getline) and push it back to the vector (myVector.push_back).
    i am not comfortable with vectors usage. any sample code would help understand better here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  2. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  5. 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