Search in file

Hi, I am wondering how to search through a text file for one specific word and display what is after it. For example, search for the word Rice: and display just the price.

And i don't how to use strstr, thank you.
Last edited on
That depends on how file is formatted.
For example if file is in format:
Beans 0.99
Carrots 1.20
Rice 3.05
Then you can use something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::string name;
std::istream file("prices.dat");
bool found = false;
while( !found && file >> name) {
    if (name == "Rice") {
        double price;
        file >> price;
        std::cout << price;
        found = true; 
    }
    std::getline(file, name); //skip rest of line
}
if (!found)
    std::cout << "Rice is not found";
the file is like this:
id initials products position price
10 RIC RICE 34 23

But in there's an error says "no matching fuction for call ro 'std::basic_istream<char>::basic_istream(const char [11])'

EDIT:

I know what was, a missing f in istream
Last edited on
And i don't how to use strstr, thank you.

We could explain this one as well while we're at it. As the first parameter "strstr()" requires a const char pointer, conveniently enough this is what the std::string.c_str() member function returns. The second parameter is a constant pointer to the string you are looking for. So in MiiNiPaa's code the only differences would be the inclusion of the cstring header and Line 5 which would be changed to:

 
if(strstr(name.c_str(), "Rice") != NULL)


That wasn't so bad was it? But really there is no reason to go including another header when you could just use std::string.compare() which looks like this:

 
if(name.compare("Rice") == 0)


Then again, since the position of the buffer matters here I almost want to say that std::string.find() is the best candidate. Oh well, dealers choice I guess.
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
#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

int main(){

string name;
string product;
ifstream file("productos.txt");
bool found = false;
cout<<"Product: ";
cin>>product;
while( !found && file >> name) {
    if (name == product) {
        double price;
        file >> price;
        cout << price;
        found = true;
    }
    getline(file, name); //skip rest of line
}
if (!found)
    cout << "Is not found";
}


But when i try to found a product always print is not found.
Just throwing this out there but you do know that the comparison is case sensitive right?
...And also is not compatible with yours file format.

So for the sake of possible future changes it is best to declare a structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct product
{
    unsigned id;
    std::string initials;
    std::string name;
    int pos;
    int /*double?*/ price;
};
//...
std::ifstream file("products.dat");
product tmp;
/*I assume that the line "id initials products position price" is not actually in file*/
while(file >> tmp.id >> tmp.initials >> tmp.name >> tmp.pos >> tmp.price)
{
    if (tmp.name == "RICE")
        std::cout << tmp.price;
}
Last edited on
Topic archived. No new replies allowed.