My problem is how to seek or search a word from my text file.
here how it done.

I want to search a word "Computer" found in my text file, and will output or will be print of screen as "Computer been found!" if the keyword are found and will show "computer not found" if the word not found inside the text file. I using fstream right now. can anybody teach me how to code such code like this, i'm a little bit of newbe about c++.

Thanks!

Recommended Answers

All 9 Replies

read the file one word at a time and compare each word read with the string. you can use fstream's extract operator >> to do that

std::string word;
std::fstream in("filename");

// read one word
in >> word;
// now compare

alternatively,

std::ifstream file("filename") ;
std::istream_iterator<std::string> begin(file), end ;
if( find( begin, end, "Computer" ) != end )
   // found

Sorry to say, but you must keep in mind the level of person when answering...
The above solutions are using STL which come a way ahead in programming in C++
A more simple way is to:

ifstream f;
f.open("filename",ios::in);
char a[15];
while(!f.eof())
f>>a;

hte above code reads from the file a word of max length 15 and puts it into the string a. The line: f>>a, tells the compiler to read from file till a white space is encountered.
For more info, go to:
http://www.bgsu.edu/departments/compsci/docs/read.html

Sorry to say, but you must keep in mind the level of person when answering...
The above solutions are using STL which come a way ahead in programming in C++
A more simple way is to:

ifstream f;
f.open("filename",ios::in);
char a[15];
while(!f.eof())
f>>a;

hte above code reads from the file a word of max length 15 and puts it into the string a. The line: f>>a, tells the compiler to read from file till a white space is encountered.
For more info, go to:
http://www.bgsu.edu/departments/compsci/docs/read.html

@above repliers with all regards...Sorry to say, but you must keep in mind the level of person when answering...
The above solutions are using STL which come a way ahead in programming in C++
A more simple way is to:

ifstream f;
f.open("filename",ios::in);
char a[15];
while(!f.eof())
f>>a;

The above code reads from the file a word of max length 15 and puts it into the string "a". The line: f>>a, tells the compiler to read from file till a white space is encountered.
For more info, go to:
http://www.bgsu.edu/departments/compsci/docs/read.html

commented: spammer +0
commented: C'mon +0

Quit spamming already. I'll give you the first 2, that was probably an accident, but the 3rd definitely was not.

also take note:
1. This thread has been dead for 2-years.
2. You did the exact same thing. You just didn't pull in the std namespace explicitly. I hope you have an appropriate namespace statement elsewhere earlier in your code.

hte above code reads from the file a word of max length 15 and puts it into the string a. The line: f>>a, tells the compiler to read from file till a white space is encountered.
For more info, go to:
http://www.bgsu.edu/departments/compsci/docs/read.html

That is actually an example of a bad way to read input (and that is also explained on the page you linked).
The page says ...

In the second example, the programmer made a mistake since the string has only space for 10 characters, but the computer would continue reading up to the letter 'p'. The actual output in this situation would depend on the computer used.

Sorry, for the above problem. Actually, I was using a really slow connection and when I clicked the post reply button, it didn't work, so out of frustration i kicked it many times which led to the above seen problem.
But the reasons apart, seeing your reputations, i don't think that you should be giving such rude replies to anybody. And I was just trying to give a solution which a newbie (as written in the problem itself) could understand easily. I was just trying to give this site something back.
Still, as I am not of enough level to argue with you and i have really got my problems solved here, I would again apologise to you people (for all the reasons you are blaming me).

commented: Here's some rep back +26

Neither my response nor mitrmkar's is rude. You may want to take a step back an look at the rest of the thread from our end. If the multi-post was an accident, it was an accident. I still stand by my comment about rezzing the thread though.

@mods:
I think closure may be in order at this point.

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.