how to confirm presence of a number or word in a text file?

Quarry's Avatar, Join Date: Jun 2008
Newbie Member
Hi, I'm new to C++ (and to programming for that matter) I need help on how to check if certain words or numbers are present in a text file.

for example:

(is the number 255 in the text file?) True or False.

thanks!
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It's not difficult, but we're not going to write the code for you. How far have you got and where are you stuck?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by xpi0t0s
It's not difficult, but we're not going to write the code for you. How far have you got and where are you stuck?
Exactly
aali's Avatar, Join Date: Jul 2008
Go4Expert Member
I think yoy should read the value from the file

Code:
int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

if(line==value)
//value that you want to compare
  else cout << "Unable to open file"; 

  return 0;
}

Last edited by shabbir; 22Jul2008 at 13:47.. Reason: Code block
aali's Avatar, Join Date: Jul 2008
Go4Expert Member
don't forget
to writel
#include<ifstream>
using namespace std;
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Use code block