1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'C++' started by Quarry, Jun 28, 2008.

  1. Quarry

    Quarry New Member

    Joined:
    Jun 28, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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!
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Exactly
     
  4. aali

    aali New Member

    Joined:
    Jul 16, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    Riyadh
    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 a moderator: Jul 22, 2008
  5. aali

    aali New Member

    Joined:
    Jul 16, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    Riyadh
    don't forget
    to writel
    #include<ifstream>
    using namespace std;
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Use code block
     

Share This Page