61 lines
1.3 KiB
C++
61 lines
1.3 KiB
C++
/*
|
|
* Íàïèñàòü ïðîãðàììó, êîòîðàÿ ïðîâåðÿåò ïðèñóòñòâóåò ëè óêàçàííîå ïîëüçîâàòåëåì
|
|
ïðè çàïóñêå ïðîãðàììû ñëîâî â óêàçàííîì ïîëüçîâàòåëåì ôàéëå (äëÿ ïðîñòîòû ðàáîòàåì
|
|
òîëüêî ñ ëàòèíèöåé). Èñïîëüçóåì ôóíêöèþ find êîòîðàÿ åñòü â ñòðîêàõ std::string.
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
const unsigned int MAX_SENTENTECE_SIZE = 100U;
|
|
|
|
int Task_5()
|
|
{
|
|
std::cout << "\t\tTask #5" << std::endl;
|
|
|
|
int result = 0;
|
|
std::string filename = "";
|
|
std::string word_to_find = "";
|
|
std::string s1 = "";
|
|
|
|
std::cout << "Enter file name where you want to find your word (without extension):\n";
|
|
std::cin >> filename;
|
|
filename += ".txt";
|
|
|
|
std::ifstream in_file(filename);
|
|
if (!in_file)
|
|
{
|
|
std::cerr << "File is not open! Check file existance!\n";
|
|
return -3;
|
|
}
|
|
|
|
std::cout << "Enter the word to find in this file: ";
|
|
std::cin >> word_to_find;
|
|
bool word_is_found = false;
|
|
|
|
while (!in_file.eof() || word_is_found != false)
|
|
{
|
|
char tmp[MAX_SENTENTECE_SIZE + 1] = {};
|
|
in_file.getline(tmp, MAX_SENTENTECE_SIZE, ' ');
|
|
s1 += tmp;
|
|
s1 += ' ';
|
|
}
|
|
if (s1.find(word_to_find) != std::string::npos)
|
|
{
|
|
word_is_found = true;
|
|
}
|
|
|
|
if (word_is_found)
|
|
{
|
|
std::cout << "Word was found!\n";
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Word was not found!\n";
|
|
}
|
|
|
|
std::cout << "________________________________________________\n\n";
|
|
system("pause");
|
|
system("cls");
|
|
return result;
|
|
} |