簡體   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