C ++读取文件搜索该文件中的某些内容

时间:2018-05-09 23:04:47

标签: c++ readfile

所以我有一个.txt文件,如下所示:
1:Meat Dish:Steak:11.5
2:Fish Dish:Fish and chips:12
第一个数字是itemNo,'Meat Dish'是我的类别,'Steak'是我的描述,最后'11 .5'是我的价格。

所以基本上我想搜索itemNo,我希望它显示该行的价格。这就是我现在所拥有的:

#include <iostream>
#include <fstream>
#include <string>
#include <vector> // We will use this to store Players
using std::string;
using std::ofstream; 
using std::ifstream; 
using std::cout;
using std::cin;

struct MenuList // Define a "Player" data structure
{
    string itemNo;
    string category;
    string descript;
    double price;
};

std::istream& operator>>(std::istream& infile, MenuList& menu)
{
    // In this function we will define how information is inputted into the player struct
    // std::cin is derived from the istream class
    getline(infile, menu.itemNo, ':'); 
    getline(infile, menu.category, ':');
    getline(infile, menu.descript, ':');
    infile >> menu.price;
    // When we have extracted all of our information, return the stream
    return infile;
}

std::ostream& operator<<(std::ostream& os, MenuList& menu)
{
    // Just like the istream, we define how a player struct is displayed when using std::cout
    os << "" << menu.itemNo << " " << menu.category << " - " << menu.descript;
    // When we have extracted all of our information, return the stream
    return os;
}

void Load(std::vector<MenuList>& r, string filename) 
{
    std::ifstream ifs(filename.c_str()); // Open the file name
    if(ifs)
    {
        while(ifs.good()) // While contents are left to be extracted
        {
            MenuList temp;
            ifs >> temp;        // Extract record into a temp object
            r.push_back(temp);  // Copy it to the record database
        }
        cout << "Read " << r.size() << " records.\n\n"; 
    }
    else
    {
        cout << "Could not open file.\n\n";
    }
}

void Read(std::vector<MenuList>& r) // Read record contents
{
    for(unsigned int i = 0; i < r.size(); i++)
        cout << r[i] << "\n";
}

void Search(std::vector<MenuList>& r) // Search records for name
{
    string n;
    cout << "Search for: ";
    cin >> n;
    for(int i = 0; i < r.size(); i++)
    {
        if(r[i].itemNo.find(n) != string::npos)
            cout << r[i];
    }
}

int main()
{
    std::vector<MenuList> records;
    Load(records, "delete.txt");
    Read(records);
    Search(records);
    return 0;
}

我真的不知道如何制作它所以只显示价格而不显示整行。

1 个答案:

答案 0 :(得分:0)

我编写了自己的代码,用于读取文本文件并将信息存储在结构中。一旦你有一个MenuList的向量,只打印你想要的东西就很简单。

include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct MenuList // Define a "Player" data structure
{
    string itemNo;
    string category;
    string descript;
    double price;
};

void addToList(vector<MenuList*>& vector_menu_list, string& itemNo, string& category, string& descript, string& price)
{
    MenuList* p_menu_list = new MenuList;
    p_menu_list->itemNo = itemNo;
    p_menu_list->category = category;
    p_menu_list->descript = descript;
    p_menu_list->price = std::stod(price);
    vector_menu_list.push_back(p_menu_list);
}

vector<MenuList*> readFile(string filename, bool& error_encountered)
{
    vector<MenuList*> vector_menu_list;

    ifstream file;
    file.open(filename);

    if (file.is_open())
    {
        string itemNo;
        string category;
        string descript;
        string price;
        short number_of_colons_encountered = 0;

        char c;
        while (file.get(c))
        {
            if ('\n' != c && EOF != c)
            {
                if (':' == c)
                {
                    number_of_colons_encountered++;
                    continue;
                }

                switch (number_of_colons_encountered)
                {
                case 0:
                    itemNo += c;
                    break;
                case 1:
                    category += c;
                    break;
                case 2:
                    descript += c;
                    break;
                case 3: 
                    price += c;
                    break;
                default:
                    error_encountered = true;
                    file.close();
                    return vector_menu_list;
                }
            }
            else
            {
                addToList(vector_menu_list, itemNo, category, descript, price);
                itemNo.clear();
                category.clear();
                descript.clear();
                price.clear();
                number_of_colons_encountered = 0;
            }
        }

        addToList(vector_menu_list, itemNo, category, descript, price);

        error_encountered = false;
    }
    else
    {
        error_encountered = true;
    }

    file.close();

    return vector_menu_list;
}
int main()
{
    bool error_encountered;
    vector<MenuList*> p_menu_list = readFile("menu list.txt", error_encountered);

    if (true == error_encountered)
    {
        return -1;
    }

    cout << "List menu items:" << endl;
    for (unsigned long long i = 0; i < p_menu_list.size(); i++)
    {
        cout << p_menu_list.at(i)->itemNo << " ";
        cout << p_menu_list.at(i)->category << " ";
        cout << p_menu_list.at(i)->descript << " ";
        cout << p_menu_list.at(i)->price << endl;
    }

    cout << "\n\nHere are only the prices: " << endl;
    for (unsigned long long i = 0; i < p_menu_list.size(); i++)
    {
        cout << p_menu_list.at(i)->price << endl;
    }

    for (unsigned long long i = 0; i < p_menu_list.size(); i++)
    {
        delete p_menu_list.at(i);
    }
    return 0;
}