繁体   English   中英

如何读取 C++ 中的逗号和引号分隔文件?

[英]How do I read comma and quotation delimited file in C++?

该文件有

CompanyNo, Items baught (3 from every company), totalpaid, totalreturn(due to bad product), relation with other company(if any(companyNo))

a787, apple, banana, strawberry, "32,888.00", "0.0" k798, g456
f238, "Orange, Juice", Kiwi, "20,666.03", 0.0
g456, "Fresh, Beef", Lamb, Chicken, "40,500.00", "4,134.00", f238

依此类推,我正在尝试将其读入 class

class Company {

    std::string CompanyNo;
    std::string Item1;
    std::string Item2;
    std::string Item3;
    double Paid;
    double Return;
    vector<std::string> RelatedTo;

public:

    Company(std::string CompanyNo, std::string Item1, std::string Item2, std::string Item3, double paid, double Return, std::vector<std::string> RelatedTo)
    {
        this->CompanyNo = CompanyNo;
        this->Item1 = Item1;
        this->Item2 = Item2;
        this->Item3 = Item3;
        this->Paid = Paid;
        this->Return = Return;
        this->RelatedTo = RelatedTo;
    };

    ~Client() {};

    Company(const Company& cn) {
        this->CompanyNo = cn.CompanyNo;
        this->Item1 = cn.Item1;
        this->Item2 = cn.Item2;
        this->Item3 = cn.Item3;
        this->Paid = cn.Paid;
        this->Return = cn.Return;
        this->RelatedTo = cn.RelatedTo;
    };

    Company() {};

    Company operator= (Company cn) {
        this->CompanyNo = cn.CompanyNo;
        this->Item1 = cn.Item1;
        this->Item2 = cn.Item2;
        this->Item3 = cn.Item3;
        this->Paid = cn.Paid;
        this->Return = cn.Return;
        this->RelatedTo = cn.RelatedTo;
        return cn;
    }

    void setCompanyNo(std::string i) { CompanyNo = i; };
    void setItem1(std::string i1) { Item1 = i1; };
    void setItem2(std::string i2) { Item2 = i2; };
    void setItem3(std::string i3) { Item3 = i3; };
    void setPaid(double p) {Paid = p; };
    void setReturn(double r) { Return = r; };
    void setRelatedTo(std::vector<std::string> rt) { RelatedTo = rt; }


    std::string getCompanyNo() const { return CompanyNo; }
    std::string getItem1() const { return Item1; }
    std::string getItem2() const { return Item2; }
    std::string getItem3() const { return Item3; }
    double getPaid() const { return Paid; }
    double getReturn() const { return Return; }
    vector<std::string> getRelatedTo() const { return RelatedTo; }

};

底部是我尝试过的,但并没有真正将每个值存储到 class 中。 这是我第一次读取具有不同列数的复杂文件和字符串之间的逗号,例如“32,888.00”应该像 32888.00 一样存储在 class 中,并且通过文件是逗号分隔的项目,例如“橙子,果汁”应该像橙子,果汁而不是那样存储橙子和果汁分开。 并且与其他公司的关系应该存储为向量,因为公司可以与许多公司或非公司有关系。 任何帮助将不胜感激谢谢

vector <Company> Info;

Company parseLine(string str)
{
    vector<string> store;
    string tok;
    stringstream evaluator;
    Company retv;

    evaluator << str;
    {
        char double_quote_remover;
        evaluator >> double_quote_remover; 
        getline(evaluator, tok, '"'); 
        char comma_remover;
        evaluator >> comma_remover; 
    }

    char ch;

    while (evaluator >> ch && ch != ','); 

    tok = ""; 
    ch = ' '; 

    while (evaluator >> ch) {  
        if (isalpha(ch)) { evaluator.putback(ch); ch = ','; } 
        if (ch == '\"') getline(evaluator, tok, '\"'); 
        else if (ch == ',') getline(evaluator, tok, ','); 
            store.push_back(tok);
        tok = ""; 
    }

    retv.setRelatedTo(store); 

    return retv; 

}

bool readFile()
{
    std::ifstream myFile("Data.txt");
    if (!myFile.is_open())
    {
        cout << "FAILED" << "\n";
        return false;
    }
    string str;

    getline(myFile, str); 

    int i = 0;
    while (std::getline(myFile, str))
    {
        Company Company = parseLine(str);
        Info.push_back(Company);
    }
    return true;
}

int main()
{
    bool data = readFile();

}

又是我!

我相信如果你只是制作一个写入结构化文件并读取它们的 function 会更容易:

#include <vector>
#include <sstream>
class Company {

    std::string CompanyNo;
    std::string Item1;
    std::string Item2;
    std::string Item3;
    double Paid;
    double Return;
    std::vector<std::string> RelatedTo;

public:

    Company(std::string CompanyNo, std::string Item1, std::string Item2, std::string Item3, double paid, double Return, std::vector<std::string> RelatedTo)
    {
        this->CompanyNo = CompanyNo;
        this->Item1 = Item1;
        this->Item2 = Item2;
        this->Item3 = Item3;
        this->Paid = paid; //there was an error here ;) you had capitalized the P in paid so it was setting itself to itself.
        this->Return = Return;
        this->RelatedTo = RelatedTo;
    };

    ~Company() {};

    Company(const Company& cn) {
        this->CompanyNo = cn.CompanyNo;
        this->Item1 = cn.Item1;
        this->Item2 = cn.Item2;
        this->Item3 = cn.Item3;
        this->Paid = cn.Paid;
        this->Return = cn.Return;
        this->RelatedTo = cn.RelatedTo;
    };

    Company() {};

    Company operator= (Company cn) {
        this->CompanyNo = cn.CompanyNo;
        this->Item1 = cn.Item1;
        this->Item2 = cn.Item2;
        this->Item3 = cn.Item3;
        this->Paid = cn.Paid;
        this->Return = cn.Return;
        this->RelatedTo = cn.RelatedTo;
        return cn;
    }

    void setCompanyNo(std::string i) { CompanyNo = i; };
    void setItem1(std::string i1) { Item1 = i1; };
    void setItem2(std::string i2) { Item2 = i2; };
    void setItem3(std::string i3) { Item3 = i3; };
    void setPaid(double p) { Paid = p; };
    void setReturn(double r) { Return = r; };
    void setRelatedTo(std::vector<std::string> rt) { RelatedTo = rt; }


    std::string getCompanyNo() const { return CompanyNo; }
    std::string getItem1() const { return Item1; }
    std::string getItem2() const { return Item2; }
    std::string getItem3() const { return Item3; }
    double getPaid() const { return Paid; }
    double getReturn() const { return Return; }
    std::vector<std::string> getRelatedTo() const { return RelatedTo; }

};


std::string WriteStructured(Company& c) {
    std::string str;
    str += '(' + c.getCompanyNo() + "):";
    str += '{' + c.getItem1() + ',' + c.getItem2() + ',' + c.getItem3() + '}';
    str += '['+std::to_string(c.getPaid())+','+std::to_string(c.getReturn())+ ']';
    str += '<';
    for (int i = 0; i < c.getRelatedTo().size(); ++i) {
        str += c.getRelatedTo()[i] + ',';
    }
    str += '>';
    return str;
}

Company ReadStructured(std::string str) {
    std::stringstream evaluator;
    Company c;
    char curr;
    std::string temp;

    evaluator << str;

    while(evaluator >> curr&&curr!='(');

    getline(evaluator, temp, ')');
    c.setCompanyNo(temp);

    while (evaluator >> curr && curr != ':');
    while (evaluator >> curr && curr != '{');

    getline(evaluator, temp, ',');
    c.setItem1(temp);
    getline(evaluator, temp, ',');
    c.setItem2(temp);
    getline(evaluator, temp, '}');
    c.setItem3(temp);

    while (evaluator >> curr && curr != '[');

    {
        double d;
        std::stringstream converter;
        getline(evaluator, temp, ',');
        converter << temp;
        converter >> d;
        c.setPaid(d);
        getline(evaluator, temp, ']');
        converter << temp;
        converter >> d;
        c.setReturn(d);
    }

    while (evaluator >> curr && curr != '<');

    {
        std::vector<std::string> Related;
        while (getline(evaluator, temp, ',')) {
            Related.push_back(temp);
        }
        c.setRelatedTo(Related);
    }

    while (evaluator >> curr && curr != '>');

    return c;
}

WriteStructured()获取 Company 并输出结构化版本, ReadStructured()获取一行并将其转换为 company。

注意:您的一个构造函数中存在错误,我用注释对其进行了标记。

如何使用函数的示例:

#include <fstream>
int main() {
    std::ifstream Input("Infile.txt");
    std::ofstream Output("Outfile.txt");
    std::string line;

    std::vector<Company> Companies; //vector of companies

    while (getline(Input, line)) //while we can still read lines
        Companies.push_back(ReadStructured(line)); //parse the lines and put them into our vector

    std::vector<std::string> Related = { "tyu9","5xyz" }; //example Related Vector
    Company c2("abc1", "Item1", "Item2", "Item3", 108.79, 3.47, Related); //example company

    line = WriteStructured(c2); //example of writing to a line, then
    Output << line; //outputting the line
}

暂无
暂无

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

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