在文本文件中搜索某个单词C ++

时间:2015-11-30 23:05:43

标签: c++ string file getline

我试图按项目的成分创建可搜索的食谱数据库。我试图创建遍历字符串向量的for循环(将每个成分保存到其中)并搜索文件并进行比较。现在,我只想输出"你好!"如果匹配。随着我所有的摆弄,无论是100 Hello!s(绝对不对)还是没有。这是代码:

int main()
{
int y;
cout << "Hello! Welcome to Abby's Recipe Calculator." << endl << endl;
cout << "Please select an option: 1 to search by ingredient or 2 to browse recipes..." << endl;
cin >> y;

vector <string> ingreds;
ingreds.reserve(4); 

if (y == 1)
{
    ingredientvector(ingreds); // calls function to fill vector w/ ingredients
}
//else if (y == 2)
//{
//call recipe function... 
//}

Search x1(ingreds); //assigns ingredients to object vector

recipesearch(x1.getingreds());

system("pause");
return 0;
}

void ingredientvector(vector<string>& x)
 {
cout << "SEARCH BY INGREDIENT" << endl;
cout << "Please enter up to three ingredients... " << endl;

for (int i = 0; i < 4; i++)
{
    x.push_back("  ");
    getline(cin, x[i]);
    if (x[i] == "1")
    {
        break;
    }

}

  }

  void recipesearch(const vector<string>& ingredientlist) //ifstream& recipes)
 {

ifstream myrecipes;
string line;
string ingredient;
myrecipes.open("recipes.txt");
if (myrecipes.is_open())
{
    for (int i = 0; i < 4; i++)
    {
        ingredient = ingredientlist[i];
        while(getline(myrecipes, line)){
            if (ingredient == line)
            {
                cout << "Hello!" << endl;
            }
            else
            {
                break;
            }
        }
    }   
}
else cout << "Unable to open recipe file!";

myrecipes.close();
}

以下是使用配方的示例:

Cheese-y Ramen

Prep Time: 5 minutes
Cook Time: 20 minutes
Total Time: 25 minutes
Servings: 2
Ingredients:
8 oz cheddar cheese
1 tablespoon cornstarch
¾ cup milk
2 packages ramen noodles
Directions:
1. Grate cheddar cheese and add with cornstarch into a small bowl
2. Combine with milk in a medium saucepan and cook on medium to low heat until consistent. Keep warm until serving.
3. In a separate pan boil ramen noodles. Set aside the included flavor packets.
4. Once boiling, drain the noodles and combine with cheese.
Recipe from Buzzfeed

2 个答案:

答案 0 :(得分:1)

这会将整个配方文件读入一个字符串,然后在每个成分的字符串中查找。注意:这是非常蛮力的。这很快,但不会非常准确。例如,如果在侧栏中提到Cheetos,而不是在配方本身中提及,则它们仍会被列出。

在它到期的地方,这个答案解除了从Read whole ASCII file into C++ std::string

批发的文件
void recipesearch(const vector<string>& ingredientlist)
{

    ifstream myrecipes;
    string file;
    myrecipes.open("recipes.txt");
    if (myrecipes.is_open())
    {
        // read entire file into string
        myrecipes.seekg(0, std::ios::end);
        file.reserve(myrecipes.tellg());
        myrecipes.seekg(0, std::ios::beg);

        file.assign((std::istreambuf_iterator<char>(myrecipes)),
                    std::istreambuf_iterator<char>());

        // look inside file string for each ingredient
        for (const string & ingredient: ingredientlist)
        {

            if (file.find(ingredient) != file.npos)
            { // found ingredient in file
                cout << ingredient << endl;
            }
        }
    }
    else
        cout << "Unable to open recipe file!";

}

警告:您将从Web中提取的大量文件采用多字节字符集编码,以获得更漂亮的结果和国际化,而不是大多数标准C ++工具默认使用的7位ASCII,包括上面示例代码中使用的那些。

正确地解释可能使用的多字节字符集中的哪一个以及如何使用它们本身就是一个讨论主题,但是为了这个分配的目的,OP可能能够确保所有输入文件都保存在ASCII编码。

答案 1 :(得分:0)

尝试反转while和for循环,如下所示:

...the code before your for loop    
while(getline(myrecipes, line))
{
    for (int i = 0; i < 4; i++)
    {
        ingredient = ingredientlist[i];
        if (ingredient == line)
        {
            cout << "Hello!" << endl;
        }
        else
        {
            break;
        }
    }
}
...the code after your for loop