Finding Words and Writing to a File

Hello everyone, I'm new in this forum and in C++.

I've searched about my question but I didn't managed to understand the solution, that's why I created this topic. Here's my question:

I want to code a program to find a word in a file, ex:.


There's a file called db.txt and inside there are names.

I want the program to searched the file for a user provided name. The user opens the program, types a name and the program searched the file for that name.

If the name is found the program displays something like "name found" if it's not found the program adds the name to the file (to the last line).
(Each line of the file has one and only one name)

That's all, thanks for reading and please help.
Also try to explain the code with commentaries because as I said I'm new to C++.
example

1
2
3
4
5
6
7
//db.txt
5
name1
name2
name3
name4
name5


in your function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool matchname = false;

void readName(){
     ifstream inFile;
     inFile.open( "db.txt" );
     if( inFile.fail() ){
           cout << "fail open " << endl;
     }
     else{
           inFile >> index;
           for( int i = 0 ; i < index ; i++ ){
                   if( yourfindname == name[index] ){
                          match = true;
                          break;
                   }
           }
            if( match ){
                    cout << "name found " << endl;
            }
            else if ( !match ){
                    cout << "record not found " << endl;
            }
}

//i already provide the inFile searching  

other try to do your self and post at here. anything will help or other senior wil reply as well
What is going on in:
inFile >> index;

and

if( yourfindname == name[index] )

?

Thanks for the fast reply.
Anyone ?
inFile is of type std::ifstream. Look at its documentation on this site, particularly operator>>.

The code is incomplete, because it does not declare index, name or yourfindname. However, we see apparent constructs:
if ( expr ) -- conditional test that evaluates expr and uses the result as a boolean.
expr1 == expr2 -- equality comparison between two expressions.
name[index] -- assuming index is int and name is array or pointer*, dereference 'name' to retrieve one value.

Have you read the tutorials that exist on this site?


*name could be a type that has operator[] too.
Hmmmm.. I just read three or two tutorial, looks like I need to read all of them.

Thanks for all the help guys, well it's going to be a fun weekend huh.

Thanks again.
just an example.
if i provide such code of this and u stil not understand

that's mean you don't have any basic on it.
i prefer u go do some experiment on it before u rush your homework

a lot code need to figure it out.
if you don't know it how to work.
u might fail in future. but of course
if have any problem u can ask us here

u try build or create a new code for yourself
and then post it here.
i will always help u then
Alright thanks :)

Also try to use Google translator or something before you post, your english isn't at its best.

Anyways thanks to everyone for replaying, I'll start reading the tutorials next weekend.

//Topic Closed (There's no need to reply anymore)
Topic archived. No new replies allowed.