简体   繁体   English

C ++。 如何从文件读取并与输入匹配

[英]C++. how to read from file and match with input

I am trying to read a textfile and matching the fruit to what I have type(eg I type apple and it will search the textfile for the word apple and match it and output that it's been found) but I am struggling to achieve the results I wanted and thus need help in it. 我正在尝试读取文本文件,并将水果与我输入的内容相匹配(例如,键入apple,它将在文本文件中搜索单词apple并将其匹配并输出找到的结果),但是我在努力实现自己的结果想要,因此需要帮助。

I have a text file (fruit.txt) with contents shown below 我有一个文本文件(fruit.txt),内容如下所示

apple,30 苹果30

banana,20 香蕉20

pear,10 梨10


this is my code 这是我的代码

string fruit;
string amount;
string line = " ";
ifstream readFile("fruit.txt");
fstream fin;

cout << "Enter A fruit: ";
cin >> fruit;

fin >> fruit >> amount;
while (getline(readFile, line,','))
    {
        if(fruit != line) {
            cout <<"the fruit that you type is not found.";
        }

       else {
           cout <<"fruit found! "<< fruit;
       }
}

please advise thanks. 请告知谢谢。

In the loop with getline you read "apple" into line the first loop, and "30\\nbanana" into line the second time, etc. 在与循环getline你看过"apple"line的第一环,而"30\\nbanana"line第二次,等等。

Instead read the whole line (using getline ), and then use eg std::istringstream to extract the fruit and amount. 而是读取整行(使用getline ),然后使用例如std::istringstream提取水果和数量。

Something like: 就像是:

std::string line;
while (std:::getline(readFile, line))
{
    std::istringstream iss(line);

    std::string fruit;
    if (std::getline(iss, fruit, ','))
    {
        // Have a fruit, get amount
        int amount;
        if (iss >> amount)
        {
            // Have both fruit and amount
        }
    }
}

I will start by saying that I am just a beginner like you, took your code and made some changes, for example: 首先,我只是像您一样的初学者,接受了您的代码并进行了一些更改,例如:

  1. Used "fstream" to read from file until is not the end of the file. 使用“ fstream”从文件读取,直到不是文件结尾。

  2. Then reading each line into a stringstream so I can brake it later using the coma delimiter. 然后将每一行读入字符串流,以便稍后可以使用逗号分隔符将其制动。

  3. I've also used a two dimension array to store the fruits and the amount of each type. 我还使用了二维数组来存储水果和每种类型的数量。

  4. In the end I had to search through the array for the fruit I wanted to display. 最后,我必须在阵列中搜索要显示的水果。

Before I post the code I want to warn you that the program wont work if there are more then 20 fruits with more than one property (in this case the amount). 在我发布代码之前,我想警告您,如果有20个以上的水果具有一个以上的属性(在这种情况下为数量),则该程序将无法工作。 Here's the code: 这是代码:

#include <sstream>
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;

void main  (void)
{
    fstream readFile("fruit.txt");
    string fruit,amount,line;
    bool found = false;
    string fruitArray[20][2];

    cout << "Enter A fruit: ";
    cin >> fruit;

    while (!readFile.eof())
    {
        int y =0 ;
        int x =0 ;
        getline(readFile,line);
        stringstream ss(line);
        string tempFruit;
        while (getline(ss,tempFruit,','))
        {
            fruitArray[x][y] = tempFruit;
            y++;
        }
        x++;
    }

    for (int i = 0; i < 20; i++)
    {
        for (int ii = 0; ii < 2; ii++)
        {
            string test = fruitArray[i][ii] ;
            if  (test == fruit)
            { 
                amount = fruitArray[i][ii+1];
                found = true;
                break;
            } 
            else{
                cout <<"Searching" << '\n';
            } 
        }
        if (found){
            cout << "Found: " << fruit << ". Amount:" << amount << endl;
            break;
        }
    }
    cout << "Press any key to exit he program.";
    getchar();
}

Hope you've learned something from it ( I sure did). 希望您从中学到了一些东西(我当然知道)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM