Searching a word in a text file

hello forum i just wanna ask how can i search a word in a textfile.. i know the basic of input and output stream... now i want is to search a word in the textfile..

my textfile contains (test.txt)
username,password
c++,cplusplus
marniel647,programmer

now i want to do is when the user inputs the marniel647 it will output the password in this case the programmer and also if the username was not found it will give a message..?

thanks in advance guys..
This can easily be done as follows:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<fstream>
#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
	ifstream fin;
	fin.open("test.txt");
	if(fin.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	string search;
	cout << "Please enter a name: ";
	cin  >> search;
	bool isFound=0;
	while(!fin.eof())
	{
		string temp = "";
		getline(fin,temp);
		for(int i=0;i<search.size();i++)
		{
			if(temp[i]==search[i])
				isFound = 1;
			else
			{
				isFound =0;
				break;
			}
		}

		if(isFound)
		{
			cout << "Password is: ";
			for(int i = search.size()+1;i<temp.size();i++)
				cout << temp[i];

			break;
		}

	}

	if(fin.eof()&&(!isFound))
	{
		cout << "Name not found!\n";
	}

	fin.close();

	return 0;
}


Sorry I did not include explanation but if you need me to explain just say so.
Topic archived. No new replies allowed.