Answers for "find word in file c++"

C++
0

how to get a word from file c++

void readFile()
{
    ifstream file;
    file.open ("program.txt");
    if (!file.is_open()) return;

    string word;
    while (file >> word)
    {
        cout<< word << '\n';
    }
}
Posted by: Guest on June-16-2021
0

search a word in cpp file

#include <iostream>
           #include <string>
           #include <fstream>
           using namespace std;
int main()
{
               
             ifstream input;
		size_t pos;
              string line;

		input.open("t.txt");
		if(input.is_open())
		{
			while(getline(input,line))
			{
			 pos = line.find("hey");
			  if(pos!=string::npos) // string::npos is returned if string is not found
        {
            cout <<"Found!";
            break;
        }
			}
		}

system("pause");
}
Posted by: Guest on January-20-2022

Browse Popular Code Answers by Language