You will first have to open the file, then read it line by line, and if the first word of the line is what you are searching for, return the rest of the line.
For more information:
http://cplusplus.com/doc/tutorial/files/
http://cplusplus.com/reference/iostream/fstream/
If you require any further assistance, please at least try by yourself and get back to us with your attempted code so we can check whether your understanding is correct or not.
#include <iostream>
#include <fstream>
#include<string.h>
#include<conio.h>
usingnamespace std;
int main () {
int x=0,m;
char line[30];
char a[50];
ifstream myfile ("example.txt") ;
if (myfile.is_open())
{
while ( myfile.good())
{
myfile.getline (line,30);
bb:
cin>>a;
int len=strlen(a);
for (int i=0;i<=30;i++)
{
if (a[0]==line[i])
{
for(int d=i;d<len+i; d++)
{
if(a[x]==line[d])
{
if(d==len-1+i)
{
cout<<a<<" ";
}
}
else
{ break;}
x=x+1;
}
x=x*0;
}
}
}
myfile.close();
}
cout<<"if you want to search another name please #PRESS 1#\nOtherwise #PRESS 0#";
cin>>m;
if(m==1)
{goto bb;
}
elsegoto cc;
cc:
getch();
return 0;
}
[file: example.txt]
red green blue
yellow
by this code i can search any string from first line but i can't search from the next line.
such as i can find red or green or blue but i can't find yellow
Thats because your code only gets the first line, and you really shouldn't use gotoat all. It's bad programming.
Also, instead of x = (x * 0), just do x = 0;.
Also, you would know how to do it if you used your brain. Go through the steps in your head, then find the steps you don't know how to do and ask us more specifically what you want. We are not going to do your homework for you.
Hint: It's in the algorithm.
And btw, i would advise that you pay more attention in class. Your code is poorly indented and formatted. It almost feels like I'm trying to read the handwriting of a 1st grader.
Sorry if I come on a bit harsh, but you should be learning these things, they are the BASICS.