-
String Search .....
Hi,
I'm suppose to search for a string in a file ...
forexample ...
The user enters the filename ... say demofile.txt
demofile.txt contains some string like ... Leon Lewis 444-44444
Here is my code:
int main()
{
ifstream infile;
fstream datafile;
char filename[81];
char prods[8][28];
char lookup[28],*strptr=NULL;
int index,i=0;
cout<<"Enter a filename: ";
cin.getline(filename, 81);
if(!infile)
{
cout<<"Cannot Open"<<filename<<endl;
}
cout<<"\nEnter string: ";
cin.getline(lookup,28);
for(index=0;index<8;index++)
{
strptr=strstr(prods[index], lookup);
if(strptr!=NULL)
{
cout <<prods[index]<< endl;
i=i+1 ;
}
continue;
}
if(i==NULL)
cout<<"\n No string found.\n";
else
cout<<"\n"<< i <<"String Found"<<endl;
return 0;
datafile.open(filename, ios::out);
return 0;
}
It opens the file but , it says no string found ...
Any help will be really appreciated ..
-
As far as I can tell without going to deep into your code, you are not even trying to open the fille.
you have to specify this to open the file:
infile.open(filename, ios::(modes that you want to open the file in) );
-
Ok now i hve opened the file , but it prints everything in the data ... forexample this is my datafile.
Leon Lewis, 444-4444
Slyvester Lewis, 999-0000
Hashim Shahid, 111-2221
Zeeshan Siddique, 777-1818
Amir Siddique, 333-1231
Ron Palmer, 234-5345
Robert King, 797-3421
Nabil Rehman, 782-3443
Now when the user enters "Lewis" i'm suppose to get :
Leon Lewis, 444-4444
Slyvester Lewis, 999-0000
I do'nt know , when i was tryin' this program without takin' filename input it was workin' fine .....
Need Help !!
Plz,
Thanxs.
-
before main()
declare struct with three stings as members-- first name, last name, and phoneNumber
in main()
declare array of the structs
declare int to act as index of array and assign it value of zero
declare ifstream and associate it with file
read data from file into array of structs, stripping away commas
search array of structs for given string be it a name or phone#
if given string found in array of struct print struc information to screen adding back the comma.
-
Can you please post some code ...
I'm like a beginner in C++ ... i got u , wat u r sayin' ... but if u post some code , it will be really helpful...
Thanxs.
-
struct Person
char firstName
char lastName
char phoneNumber
int main()
Person book
int index = 0;
char filename
ifstream fin(filename)
if !fin
cout unable to open file
while !fin.eof()
fin.getline(book[index].firstname, , space)
getiline(lastName, , comma)
getline(phoneNumber, , newlinechar)
index++
index--
int i;
char search
cout enter last name to search for
cin search
for(i = 0; i < index; i++)
if strcmp book[i].lastname search is zero
cout firstname space lastname comma space phone number newline
return 0
You can wrap it in a loop using a flag the user can change so the user can do more than one search per run of the program. You can use a flag that is changed if a match is found. After searching the entire container if the flag hasn't been changed then no match was found.