[Solved] How do you make a c++ program search for a string? [closed]


First open an ifstream to open your file then check for the string:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      if(line.find("the string to find") != string::npos)
      {
         //line found, do something
      }
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

3

solved How do you make a c++ program search for a string? [closed]