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 5990677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:16:42+00:00 2026-05-22T23:16:42+00:00

Im trying to use the string::find method to determine if the string hello (with

  • 0

Im trying to use the string::find method to determine if the string ” hello ” (with space before and after) exists in a line of a .txt file. if it does, its supposed to print out the line number(position isnt important). the problem is, its not finding the string. please help.

int main() {
    string key (" hello ");
    ifstream myReadFile;
    myReadFile.open("test.txt");
    string s;
    int lineCount = 1;
    int found;
    if (myReadFile.is_open()) {
        while (!myReadFile.eof()) {
            getline(myReadFile, s);
            found = s.find(key);
            if(found != string::npos) {
                cout<<lineCount<<endl;
                lineCount++;
            }
         }
    }
    myReadFile.close();
    return 0;
}
  • 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-22T23:16:43+00:00Added an answer on May 22, 2026 at 11:16 pm

    What it seems to be doing as you have it is just counting the number of lines that have that string in them. You should increment the line number var in every iteration of the loop, not just when the string is found.

    int main() {
        std::string key (" hello ");
        ifstream myReadFile;
        myReadFile.open("test.txt");
    
    
        if (myReadFile) {
    
            std::string line;
            int line_number = 0;
            while (std::getline(myReadFile, line)) {
                line_number++;                
                if (line.find(key) != std::string::npos)
                    std::cout << line_number << std::endl;
            }
    
        } else {
            std::cout << "Error opening file\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use ResourceBundle#getStringArray to retrieve a String[] from a properties file.
I am trying to use the List.Find method to find a match with an
I' trying to use a Linq query to find and set the selected value
I'm trying to find a way to determine how many parameters a constructor has.
I am trying to determine whether a string input by a user is valid
I'm trying to mock a data layer method. The method takes a string and
I'm trying to use Spring IoC with an interface like this: public interface ISimpleService<T>
I'm still trying to decide whether my (home) project should use UTF-8 strings (implemented
Trying to use an excpetion class which could provide location reference for XML parsing,
Trying to use a guid as a resource id in a rest url but

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.