Like Tree1Likes
  • 1 Post By Khabz

getline by line

This is a discussion on getline by line within the C++ Programming forums, part of the General Programming Boards category; i am trying to get text file and read only first 100 lines of the text content using c/c++. how ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    162

    Thumbs up getline by line

    i am trying to get text file and read only first 100 lines of the text content using c/c++. how can i do it?

    is it something like string line;
    getline(stream,line);



    ??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    You are on the right track, but you may also want a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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).

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    162
    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.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Learn some more about them, they are essential.
    vector - C++ Reference

    To store the first line in a vector:
    Code:
    string line;
    vector<string> MyVector;
    ifstream MyFile("example.txt");
    
    getline(MyFile, line); //Gets the first line of the file
    MyVector.push_back(line); //Pushes it to the vector
    Elysia likes this.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    1
    Quote Originally Posted by leo2008 View Post
    i am trying to get text file and read only first 100 lines of the text content using c/c++. how can i do it?

    is it something like string line;
    getline(stream,line);



    ??
    If you provide us with some code then we can help guide you towards your goal.

    I would recommend breaking down your problem into steps.

    1. Do something 'x' ammounts of times
    2. For each x, read at the location
    - the location should be a line

    Based on the above steps, it would look as if you would need something to loop for step 1, and within the loop, you would need to
    do a read call, which involves reading a sentence. I would search around the site at I/O and it will help you with understanding how to read a line of text from a text file.

    Please post your solution once you are done in here so that others may benefit from it.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    162

    Thumbs up

    Quote Originally Posted by d4n1 View Post
    If you provide us with some code then we can help guide you towards your goal.

    I would recommend breaking down your problem into steps.

    1. Do something 'x' ammounts of times
    2. For each x, read at the location
    - the location should be a line

    Based on the above steps, it would look as if you would need something to loop for step 1, and within the loop, you would need to
    do a read call, which involves reading a sentence. I would search around the site at I/O and it will help you with understanding how to read a line of text from a text file.

    Please post your solution once you are done in here so that others may benefit from it.
    some code which i tried with, but actually here it prints on the standard i/o which i dont want to...

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string str;
      ifstream ifs("test.txt");
      getline (ifs,str);
      cout << "the first line of the file is " << str << ".\n";
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Standard I/O is just I/O like any other file. If you can work with stadnard i/o (ie cout, cin), then you can work with files too.
    Just use the correct stream for reading and writing. It will be exactly the same otherwise (except standard streams are always open; files you have to open manually).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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, 06:32 AM
  3. Replies: 7
    Last Post: 12-13-2010, 01:13 PM
  4. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 08: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

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