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

Newbie Member
28Jun2008,11:24   #1
Quarry's Avatar
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!
Mentor
16Jul2008,16:05   #2
xpi0t0s's Avatar
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?
Go4Expert Founder
16Jul2008,19:56   #3
shabbir's Avatar
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
Go4Expert Member
22Jul2008,13:31   #4
aali's Avatar
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
Go4Expert Member
22Jul2008,13:33   #5
aali's Avatar
don't forget
to writel
#include<ifstream>
using namespace std;
Go4Expert Founder
22Jul2008,13:48   #6
shabbir's Avatar
Use code block