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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:28:46+00:00 2026-06-13T06:28:46+00:00

Here is the code I used to detect the string in a line from

  • 0

Here is the code I used to detect the string in a line from a txt file:

int main()
{
    std::ifstream file( "C:\\log.txt" );

    std::string line;
    while(!file.eof())
    {
        while( std::getline( file, line ) )   
        {
            int found = -1;
            if((found = line.find("GetSA"))>-1)
                std::cout<<"We found GetSA."<<std::endl;
            else if ((found = line.find("GetVol"))>-1)
                std::cout<<"We found GetVol."<<std::endl;
            else if ((found = line.find("GetSphereSAandVol"))>-1)
                std::cout<<"We found GetSphereSAandVol."<<std::endl;
            else
                std::cout<<"We found nothing!"<<std::endl;

        }
    }
    std::cin.get();
}

And here is my log file:

GetSA (3.000000)

GetVol (3.000000)

GetSphereSAandVol (3.000000)

GetVol (3.000000)

GetSphereSAandVol (3.000000)

GetSA (3.00000)

The error is, the program will not go to find “GetSphereSAandVol”, because it stops at “GetSA”. Obviously, the program thinks “GetSphereSAandVol” contains “GetSA”, so it will execute:

if(found = line.find("GetSA"))
    std::cout<<"We found GetSA."<<std::endl;

which is not exactly what I want, because I am expecting the program to execute:

else if (found = line.find("GetSphereSAandVol"))
    std::cout<<"We found GetSphereSAandVol."<<std::endl;

So, anyway I can avoid this? to get what I really want? Thanks a lot.

  • 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-13T06:28:47+00:00Added an answer on June 13, 2026 at 6:28 am

    You misunderstand how find works. Read the documentation.

    The conditionals should go like this:

    if ((found = line.find("xyz")) != line.npos) { /* found "xyz" */ }
    

    I would write your entire program like this:

    int main(int argc, char * argv[])
    {
        if (argc != 2) { std::cout << "Bad invocation\n"; return 0; }
    
        std::ifstream infile(argv[1]);
    
        if (!infile) { std::cout << "Bad filename '" << argv[1] << "'\n"; return 0; }
    
        for (std::string line; std::getline(infile, line); )
        {
            int pos;
    
            if ((pos = line.find("abc")) != line.npos)
            {
                std::cout << "Found line 'abc'\n";
                continue;
            }
    
            if ((pos = line.find("xyz")) != line.npos)
            {
                std::cout << "Found line 'xyz'\n";
                continue;
            }
    
            // ...
    
            std::cout << "Line '" << line << "' did not match anything.\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've used the following code, taken from here , to create my own custom
Here is a section of code used by CKEditor on my website: CKEDITOR.config.IPS_BBCODE =
here the following code is used to view the present modal view controller. [[self
Here is the code I used. library(xlsx) wb <- loadWorkbook('D:/test.xls') sheets <- getSheets(wb) sheet
I used the code described here but now, when I do a for ...
i've used jquery load method for a website pagination here is my code function
I am using the sample provided here and have used the code inside a
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
Here is the condition I used to detect if we are dealing with a
Here is a piece of code from the jQuery source (bit.ly/jqsource): // The DOM

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.