I need to check if a word exists in a dictionary text file, I think I could use strcmp, but I don't actually know how to get a line of text from the document. Here's my current code I'm stuck on.
#include "includes.h"
#include <string>
#include <fstream>
using namespace std;
bool CheckWord(char* str)
{
ifstream file("dictionary.txt");
while (getline(file,s)) {
if (false /* missing code */) {
return true;
}
}
return false;
}
std::string::find
does the job.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool CheckWord(char* filename, char* search)
{
int offset;
string line;
ifstream Myfile;
Myfile.open (filename);
if (Myfile.is_open())
{
while (!Myfile.eof())
{
getline(Myfile,line);
if ((offset = line.find(search, 0)) != string::npos)
{
cout << "found '" << search << "' in '" << line << "'" << endl;
Myfile.close();
return true;
}
else
{
cout << "Not found" << endl;
}
}
Myfile.close();
}
else
cout << "Unable to open this file." << endl;
return false;
}
int main ()
{
CheckWord("dictionary.txt", "need");
return 0;
}
char aWord[50];
while (file.good()) {
file>>aWord;
if (file.good() && strcmp(aWord, wordToFind) == 0) {
//found word
}
}
You need to read words with the input operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With