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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:15:49+00:00 2026-05-25T18:15:49+00:00

This is some code I wrote to check a string’s presence in a file:

  • 0

This is some code I wrote to check a string's presence in a file:

bool aviasm::in1(string s)
{
ifstream in("optab1.txt",ios::in);//opening the optab
//cout<<"entered in1 func"<<endl;
char c;
string x,y;
while((c=in.get())!=EOF)
{
    in.putback(c);
    in>>x;
    in>>y;
    if(x==s)
    return true;
}
return false;
}

it is sure that the string being searched lies in the first column of the optab1.txt and in total there are two columns in the optab1.txt for every row.
Now the problem is that no matter what string is being passed as the parameter s to the function always returns false. Can you tell me why this happens?

  • 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-25T18:15:49+00:00Added an answer on May 25, 2026 at 6:15 pm

    What a hack! Why not use standard C++ string and file reading functions:

    bool find_in_file(const std::string & needle)
    {
      std::ifstream in("optab1.txt");
      std::string line;
    
      while (std::getline(in, line))  // remember this idiom!!
      {
        // if (line.substr(0, needle.length()) == needle)  // not so efficient
        if (line.length() >= needle.length() && std::equal(needle.begin(), needle.end(), line.begin())) // better
        // if (std::search(line.begin(), line.end(), needle.begin(), needle.end()) != line.end())  // for arbitrary position
        {
          return true;
        }
      }
      return false;
    }
    

    You can replace substr by more advanced string searching functions if the search string isn’t required to be at the beginning of a line. The substr version is the most readable, but it makes a copy of the substring. The equal version compares the two strings in-place (but requires the additional size check). The search version finds the substring anywhere, not just at the beginning of the line (but at a price).

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

Sidebar

Related Questions

I wrote some code and tried the Ctrl + T to check transpose feature
Can someone write some sample code to explain this concept? I know what a
This is some code I found on the internet. I'm not sure how it
this is some code that SDL requires in visual studios 2005 in order for
I recently came across this in some code - basically someone trying to create
I'm currently doing something like this in some code I'm working on right now:
Context I came across some code, like this: if( Some_Condition ) throw 0; I
const static int foo = 42; I saw this in some code here on
I just found this in some old code, and I'm not sure what it
Has anyone done this / have some example code?

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.