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

  • Home
  • SEARCH
  • 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 8561947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:37:11+00:00 2026-06-11T16:37:11+00:00

Is there a particular function in c++ that can return the line number of

  • 0

Is there a particular function in c++ that can return the line number of a particular string i want to find?

ifstream fileInput;
int offset;
string line;
char* search = "a"; // test variable to search in file
// open file to search
fileInput.open(cfilename.c_str());
if(fileInput.is_open()) {
    while(!fileInput.eof()) {
        getline(fileInput, line);
        if ((offset = line.find(search, 0)) != string::npos) {
            cout << "found: " << search << endl;
        }
    }
    fileInput.close();
}
else cout << "Unable to open file.";

I want to add some codes at:

    cout << "found: " << search << endl;

That will return the line number followed by the string that was searched.

  • 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-06-11T16:37:12+00:00Added an answer on June 11, 2026 at 4:37 pm

    Just use a counter variable to keep track of the current line number. Each time you call getline you… read a line… so just increment the variable after that.

    unsigned int curLine = 0;
    while(getline(fileInput, line)) { // I changed this, see below
        curLine++;
        if (line.find(search, 0) != string::npos) {
            cout << "found: " << search << "line: " << curLine << endl;
        }
    }
    

    Also…

    while(!fileInput.eof())

    should be

    while(getline(fileInput, line))

    If an error occurs while reading eof will not be set, so you have an infinite loop. std::getline returns a stream (the stream you passed it) which can be implicitly converted to a bool, which tells you if you can continue to read, not only if you are at the end of the file.

    If eof is set you will still exit the loop, but you will also exit if, for example, bad is set, someone deletes the file while you are reading it, etc.

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

Sidebar

Related Questions

Is there MS SQL Server function that counts the number of times a particular
Is there any way to find particular C language function's input and output parameters
Is there a library function that performs binary search on a list/tuple and return
Is there a way I can find all the variables in Ruby that begin
Is there any particular reason that all data members in a class are private
There are many systems that depend on the uniqueness of some particular value. Anything
Is there a way to obtain a particular bit in a hexadecimal number to
I have a function that checks for a particular event and when it detects
Is there any particular way to integrate Captch code validation in iPhone application? is
Is there a particular way to initialize an IList<T> ? This does not seem

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.