简体   繁体   English

C ++逐行读取逗号分隔的文件(其中一个部分为mm / dd / yyyy),并安全地放入结构中

[英]C++ reading in a comma delimited file[with one section as mm/dd/yyyy] by line, placing into a struct securely

To expand on the title, I am reading a file line-by-line that appears as so: 为了扩展标题,我正在逐行阅读一个文件,该文件如下所示:

FirstName,LastName,mm/dd/yyyy,SSN,Role,Salary,Zip,Phone

I have this code I just wrote but am having some trouble placing it into my struct as I'm using std::string as opposed to char[]. 我有我刚刚编写的这段代码,但是在使用std :: string而不是char []时,将其放入结构时遇到了一些麻烦。 I want to continue using std::string for purposes down the road. 我想继续使用std :: string来达到目的。 Also, excuse any syntax errors as I haven't written in c/c++ in a while. 另外,请原谅任何语法错误,因为我已经有一段时间没有用c / c ++编写了。 I have also read the most elegant way to iterate to words of a string but I am still confused on how to do that with the slashes involved in the date format. 我还阅读了最优雅的方式来迭代字符串中的单词,但是我仍然对如何使用日期格式中的斜杠感到困惑。 SSN and Salary are private members of a struct that will be pushed into a vector for later use. SSN和Salary是结构的私有成员,该结构将被压入向量中以备后用。 How can I do this using c++ libraries? 如何使用c ++库执行此操作? To be honest, the istringstream confuses me as they include some type of parser inside their struct directly. 老实说,istringstream使我感到困惑,因为它们直接在结构中包含某种类型的解析器。 Is this honestly the best way to accomplish what I'm trying to do? 老实说,这是完成我要完成的工作的最佳方法吗?

char stringData[150]; //line to be read in 
while(fgets(stringData, 150, infile) != NULL) {
    if( currentLine == 1) {
        fgets(stringData, 150, infile); //get column names | trash
    }
    else {
        lineSize = sscanf(stringData, "%[^,],%[^,],%d/%d/%d,%d,%[^,],%lf,%[^,],%s", temp.firstName,temp.lastName,
                       &temp.birthMonth,&temp.birthDay,&temp.birthYear, 
                       &tempSSN, temp.role, &tempSalary, temp.zip,
                       temp.phoneNum);

        if(lineSize != 10) { //error message due to a row being incorrect
            cerr << "/* ERROR: WRONG FORMAT OF INPUT(TOO FEW OR TOO MANY ARGUMENTS) ON LINE: */" << currentLine << '\n';
            exit(1);
        }

        temp.setSSN(tempSSN);
        temp.setSalary(tempSalary);
        vector.push_back(temp);//push Employee temp into the vector and repeat loop
    }
    currentLine++
}

TL;DR: What is the easiest way to do this using c++ libraries? TL; DR:使用c ++库执行此操作的最简单方法是什么?

As Sam Varshavchik already mentioned, the easiest way would be separating input with , then separate on of them with / again. 正如Sam Varshavchik所述,最简单的方法是使用分隔输入,然后再次使用/分隔输入。

Thanks to this famous question I'm using following approach to split string : 由于这个著名的问题,我正在使用以下方法来分割字符串:

template<typename Out>
void split(const std::string &s, char delim, Out result)
{
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim))
    {
        *(result++) = item;
    }
}

std::vector<std::string> split(const std::string &s, char delim)
{
    std::vector<std::string> elems;
    split(s, delim, std::back_inserter(elems));
    return elems;
}

assuming that this is your structure : 假设这是您的结构:

struct info
{
    std::string firstName;
    std::string lastName;
    std::string birthMonth;
    std::string birthDay;
    std::string birthYear;
    std::string tempSSN;
    std::string role;
    std::string tempSalary;
    std::string zip;
    std::string phoneNum;

};

I would implement your needed function like this : 我将像这样实现您所需的功能:

void parser(std::string fileName, std::vector<info> &inf)
{
    std::string line;
    std::ifstream infile(fileName);
    int index = inf.size();
    while(std::getline(infile, line))
    {
        inf.push_back({});
        std::vector<std::string> comma_seprated_vec = split(line, ',');
        inf.at(index).firstName = comma_seprated_vec.at(0);
        inf.at(index).lastName = comma_seprated_vec.at(1);
        inf.at(index).tempSSN = comma_seprated_vec.at(3);
        inf.at(index).role = comma_seprated_vec.at(4);
        inf.at(index).tempSalary = comma_seprated_vec.at(5);
        inf.at(index).zip = comma_seprated_vec.at(6);
        inf.at(index).phoneNum = comma_seprated_vec.at(7);
        std::vector<std::string> slash_seprated_vec = split(comma_seprated_vec.at(2), '/');
        inf.at(index).birthMonth = slash_seprated_vec.at(0);
        inf.at(index).birthDay = slash_seprated_vec.at(1);
        inf.at(index).birthYear = slash_seprated_vec.at(2);
        ++index;
    }
}

Then you can use it like this : 然后,您可以像这样使用它:

int main()
{
    std::vector<info> information;
    parser("some file", information);

    return 0;
}

There you go, your information are presented in information variable. 到这里,您的信息就会显示在information变量中。

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

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