Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:06:56+00:00 2026-05-13T08:06:56+00:00

Hi I have a file with some text in it. Is there some easy

  • 0

Hi I have a file with some text in it. Is there some easy way to get the number of lines in the file without traversing through the file?

I also need to put the lines of the file into a vector. I am new to C++ but I think vector is like ArrayList in java so I wanted to use a vector and insert things into it. So how would I do it?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:06:57+00:00Added an answer on May 13, 2026 at 8:06 am

    You would need to traverse the file to detect the number of lines (or at least call a library method that traverse the file).

    Here is a sample code for parsing text file, assuming that you pass the file name as an argument, by using the getline method:

    #include <string>
    #include <vector>
    #include <fstream>
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
      std::vector<std::string> lines;
      std::string line;
    
      lines.clear();
    
      // open the desired file for reading
      std::ifstream infile (argv[1], std::ios_base::in);
    
      // read each file individually (watch out for Windows new lines)    
      while (getline(infile, line, '\n'))
      {
        // add line to vector
        lines.push_back (line);
      }
      // do anything you like with the vector.  Output the size for example:
      std::cout << "Read " << lines.size() << " lines.\n";
    
      return 0;
    }
    

    Update: The code could fail for many reasons (e.g. file not found, concurrent modifications to file, permission issues, etc). I’m leaving that as an exercise to the user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file with some text information and i need to split this
I have a .txt file having some text. I want to read this file
I have text file with some stuff that i would like to put into
I have a .rc file which is used to include some text data in
I have this html file where I want to overlay some text over another.
I have a plain text file looking like this: some text containing line breaks
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I have some status text displayed in a BASH script, e.g.: Removed file sandwich.txt.
I have a simple WPF (XAML) file that has some animated shapes and text.
okay, so i have a text file with example content below line1with some random

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.