如何将此文件读入变量?

时间:2012-04-22 22:09:44

标签: c++

这是我需要阅读的文件。

Coca-Cola,0.75,20
Root Beer,0.75,20
Sprite,0.75,20
Spring Water,0.80,20
Apple Juice,0.95,20

我需要将每个变成一个变量。 例如drinkName,drinkCost,drinkQuantity。 我正在使用c ++,请帮助我。

3 个答案:

答案 0 :(得分:6)

我建议将这三个变量捆绑到一个数据类型中。 让我们称之为“DrinkT”。

struct DrinkT{
    std::string name;
    float cost;
    unsigned int quantity;

    DrinkT(std::string const& nameIn, float const& costIn, unsigned int const& quantityIn):
    name(nameIn),
    cost(costIn),
    quantity(quantityIn)
    {}
};

这有很多优点 您现在可以创建这样的饮料:

DrinkT coke("Coca-Cola",0.75,20);

并访问如下变量:

std::cout << coke.name << std::endl;     //outputs: Coca-Cola
std::cout << coke.cost << std::endl;     //outputs: 0.75
std::cout << coke.quantity << std::endl; //outputs: 20

饮品只能保留您指定的饮品:饮品名称,饮料成本和饮品数量 该对象有一个构造函数,可以在构造中获取所有这三个值 这意味着,如果同时指定所有值,则只能创建饮品对象。

如果您要存放大量饮料(并且您可能不知道有多少),我们可能还想将它们放入某种容器中。
矢量是一个不错的选择。矢量将增长到占据您想要存储的饮料数量。

让我们遍历文件,分别读入三个值,并将它们存储在我们的向量中。我们会一次又一次地为每种饮料做这件事,直到我们到达文件的末尾。

int main(){

    std::ifstream infile("file.txt");
    std::vector<DrinkT> drinks;

    std::string name;
    std::string cost;
    std::string quantity;

    std::getline(infile,name,',');
    std::getline(infile,cost,',');
    std::getline(infile,quantity,' ');

    while (infile){
        drinks.push_back(DrinkT(name,atof(cost.c_str()),atoi(quantity.c_str())));

        std::getline(infile,name,',');
        std::getline(infile,cost,',');
        std::getline(infile,quantity,' ');
    }


    //output
    for(DrinkT drink : drinks){
        std::cout << drink.name << " " << drink.cost << " " << drink.quantity << std::endl;
    }

    return EXIT_SUCCESS;
}
  

使用g ++ -std = c ++ 0x -o main main.cpp

编译

有关使用的一些语言功能的信息:
http://www.cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/stl/vector/

答案 1 :(得分:3)

在一些(但绝对不是全部)方式中,我的建议大致类似于@ Xploit。我首先要定义一个结构(或类,如果你愿意)来保存数据:

struct soft_drink { 
    std::string name;
    double price;
    int quantity;
};

然后(主要区别)我为该类/结构定义operator>>

std::istream &operator>>(std::istream &is, soft_drink &s) { 
    // this will read *one* "record" from the file:

    // first read the raw data:
    std::string raw_data;
    std::getline(raw_data, is);
    if (!is)
        return is; // if we failed to read raw data, just return.        

    // then split it into fields:
    std::istringstream buffer(raw_data);

    std::string name;
    std::getline(buffer, name, ',');

    double price = 0.0;
    buffer >> price;
    buffer.ignore(1, ',');

    int quantity = 0;
    buffer >> quantity;

    // Check whether conversion succeeded. We'll assume a price or quantity 
    // of 0 is invalid:
    if (price == 0.0 || quantity = 0)
        is.setstate(std::ios::failbit);

    // Since we got valid data, put it into the destination:
    s.name = name;
    s.price = price;
    s.quantity = quantity;
    return is;
}

这让我们从输入流中读取一条记录,知道转换是否成功。一旦我们有了读取一条记录的代码,剩下的就变得微不足道了 - 我们可以使用一对正确类型的istream_iterator来初始化数据向量:

// read the data in one big gulp (sorry, couldn't resist).
//
std::vector<soft_drink> drink_data((std::istream_iterator<soft_drink>(infile)), 
                                    std::istream_iterator<soft_drink>());

答案 2 :(得分:1)

首先调查如何将一个项目与其他项目分开:

How to split a string in C++?(通过空格)

之后,您将获得一个格式为drinkName,drinkCost,drinkQuantity

的新字符串

如果您考虑一下,将项目的每个信息分开的是符号,(逗号),因此您需要检查this post,因为它还显示了如何用逗号分隔。

为了帮助存储此信息,您可以创建一个包含3个变量的新数据类型(类):

class Drink
{
public:
    std::string name;
    std::string value;     // or float
    std::string quantity;  // or int
};

最后,你可以拥有一个包含所有信息的好std::vector<Drink>

相关问题