C++ 如何对齐矢量(已删除的双引号)并在c++;在读取txt文件之后?

C++ 如何对齐矢量(已删除的双引号)并在c++;在读取txt文件之后?,c++,C++,我有一个txt文件,包括字符串、int和float。我使用一个向量来存储它们。 该文件可以读取和打印。但是向量没有对齐,双引号也没有删除 这是我的密码: #include<iostream> #include <string> #include <vector> #include <fstream> #include <stdlib.h> #include "assignment1.h" using namespa

我有一个txt文件,包括字符串、int和float。我使用一个向量来存储它们。 该文件可以读取和打印。但是向量没有对齐,双引号也没有删除

这是我的密码:

#include<iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include "assignment1.h"

using namespace std;

vector <assignment1*> rsult;

assignment1* parseLine(std::string str)
{
    vector<string>store;
    string tok = "";
    assignment1* retv = nullptr;

    for (int i = 0; i < (int)str.size(); i++)
    {
        char c = str[i];
        if (c != ',')
        {
            tok = tok + c;
        }
        else
        {
            store.push_back(tok);
            tok = "";
        }
    }
    if (tok != "") { store.push_back(tok); }
    
    retv = new assignment1(store[0], store[1], store[2], store[3], store[4], store[5], store[6]);

    //id=store[0]
    //name=store[1]
    //postcode=store[2]
    //city=store[3]
    //purchases=store[4]
    //returns=store[5]
    //cIDs=store[6]

    return retv;
}

bool readFile()
{

    std::ifstream myFile("Data1.txt");
    if (!myFile.is_open())
    {
        cout << "It failed to open the file" << endl;
        return false;
    }

    std::string str;

    int i = 0;
    while (std::getline(myFile, str))
    {
        //cout << "DEBUG -- line " << ++i << " is:" << str << endl;
        if (str[0] != '/')
        {
            assignment1* assignment1 = parseLine(str);
            rsult.push_back(assignment1);
        }
    }

    for (assignment1* t : rsult)
    {
        t->show();
    }

    return true;
}

void assignment1::show()
{
    cout << id << setw(10) << name << setw(10) << postcode << setw(10) << city << setw(10) << purchases << setw(10) << returns << setw(10) << cIDs << endl;
}
我打印的结果如下所示:

U2123,Sam Inc,2006, lyons,"21,600.00",13.10,123
A721,Merry Inc,2604, Kingston,"21,600.10",103.00,
U2122,Pippin Inc,2612, reid,"21,600.00",0
U2123   Sam Inc      2006     lyons       "21   600.00"     13.10
A721 Merry Inc      2604  Kingston       "21   600.10"    103.00
U2122Pippin Inc      2612      reid       "21   600.00"         0
我想要的结果是:

U2123     Sam Inc      2006     lyons       21    600.00     13.10
A721    Merry Inc      2604  Kingston       21    600.10    103.00
U2122  Pippin Inc      2612      reid       21    600.00         0

问题一是您的CSV解析器(
parse_line
)需要能够处理双引号。您必须忽略双引号中出现的逗号,这意味着添加一个额外的状态变量,说明您是否在双引号中。因为显示您想要的结果肯定不是您真正想要的。您希望“21600.00”显示为一列。当您标记行时,您需要处理引号并忽略引号内出现的逗号。您可以通过读取该行,然后逐字符读取,如果您发现一个引号作为字符串的一部分处理任何后续逗号,那么当发现下一个引号时,继续使用逗号断开。