I have a question:

Say I have a text file with a bunch of words on it, say the following:

hello
baby
crap
hate
ground
soul
fart
hot
render

Now, if I make a program that requires the user to input a word, how would I go about searching the text file and seeing if that word is there?

I haven't any code for this yet. I know how to open/close text files, as well as read from them and output their contents, but to search them...I dunno.

Help appreciated and thanks in advance.

Recommended Answers

All 3 Replies

You would have to read the file word-by-word and compare the read-in word with the user input to see if it matches.

Alternatively, you could read the file (again, word-by-word) into a vector of strings, then use the find() function from the <algorithm> header.

Im a BSc IT student. would anyone help me on how to make and use a file stream in detail please. I am desparate since i have a test and an exam coming up in a couple of weeks.

Explaining how file streams work in detail is not going to be something that anybody here really wants to do. What you should do is study up on file IO (Using your book, interwebs etc.), write some test code and post it with any questions/ problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.