C++将数据读为一系列结构,从OS SigSEV生成错误

发布于 2025-02-03 07:53:50 字数 1322 浏览 1 评论 0原文

我正在尝试从.CSV读取数据(据我所知,需要Getline)。我什至无法开始弄清楚我在这里做错了什么。

我的第一个功能应该打开文件并将数据拉到数组,位置[]。但是,一旦我击中“ positionsArray [i] .agency =“调试时,我会遇到一个错误:下等式停止,因为它从操作系统sigsegv收到了信号。我不知道是什么原因造成的,也许我没有正确创建或引用的数组。

void getData(std::ifstream& positionsListStream, Position positionsArray[], int streamSize) {
    std::string paredEntry, lineTemp, positionsList;
    std::string agency, classification, fullTime;
    std::string::size_type index = 0;
    double salary;
    for(int i = 0; i < streamSize; i ++) {
        getline(positionsListStream, lineTemp);
        index = lineTemp.find(",");
        agency = lineTemp.substr(0, index);
        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        classification = lineTemp.substr(0, index);
        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        fullTime = lineTemp.substr(0, index);
        lineTemp.erase(0, (index + 1));
        salary = salaryToDouble(lineTemp);
        positionsArray[i].agency = agency;
        positionsArray[i].classification = classification;
        positionsArray[i].fullTime = fullTime;
        positionsArray[i].salary = salary;
    }
    return;
}

主要调用此功能

    getData(positionsListStream, &positionsArray[streamSize], streamSize);

I'm trying to read data from a .csv (therefore necessitating getline as far as I know). I can't even begin to figure out what I've done wrong here.

My first function is supposed to open the file and pull the data into an array, positionsArray[]. However, as soon as I hit "positionsArray[i].agency = " when debugging, I get an error: The inferior stopped because it received a signal from the operating system SIGSEGV. I have no idea what's causing this other than maybe I don't have the array created or referenced properly.

void getData(std::ifstream& positionsListStream, Position positionsArray[], int streamSize) {
    std::string paredEntry, lineTemp, positionsList;
    std::string agency, classification, fullTime;
    std::string::size_type index = 0;
    double salary;
    for(int i = 0; i < streamSize; i ++) {
        getline(positionsListStream, lineTemp);
        index = lineTemp.find(",");
        agency = lineTemp.substr(0, index);
        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        classification = lineTemp.substr(0, index);
        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        fullTime = lineTemp.substr(0, index);
        lineTemp.erase(0, (index + 1));
        salary = salaryToDouble(lineTemp);
        positionsArray[i].agency = agency;
        positionsArray[i].classification = classification;
        positionsArray[i].fullTime = fullTime;
        positionsArray[i].salary = salary;
    }
    return;
}

main calls this function with

    getData(positionsListStream, &positionsArray[streamSize], streamSize);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2025-02-10 07:53:50

只需使用std :: vector&lt; position&gt;而忘记内存管理:

std::vector<Position> getData(std::ifstream& positionsListStream, int streamSize) {
    std::vector<Position> positionsArray;
    std::string agency, classification, fullTime;
    double salary;
    for(int i = 0; i < streamSize; i ++) {
        Position p;
        std::string::size_type index = 0;
                std::string lineTemp;

        getline(positionsListStream, lineTemp);
        index = lineTemp.find(",");
        p.agency = lineTemp.substr(0, index);

        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        p.classification = lineTemp.substr(0, index);

        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        p.fullTime = lineTemp.substr(0, index);

        lineTemp.erase(0, (index + 1));
        p.salary = salaryToDouble(lineTemp);

        positionsArray.push_back(p);
    }
    return positionsArray;
}

Just use a std::vector<Position> and forget about memory management:

std::vector<Position> getData(std::ifstream& positionsListStream, int streamSize) {
    std::vector<Position> positionsArray;
    std::string agency, classification, fullTime;
    double salary;
    for(int i = 0; i < streamSize; i ++) {
        Position p;
        std::string::size_type index = 0;
                std::string lineTemp;

        getline(positionsListStream, lineTemp);
        index = lineTemp.find(",");
        p.agency = lineTemp.substr(0, index);

        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        p.classification = lineTemp.substr(0, index);

        lineTemp.erase(0, (index + 1));
        index = lineTemp.find(",");
        p.fullTime = lineTemp.substr(0, index);

        lineTemp.erase(0, (index + 1));
        p.salary = salaryToDouble(lineTemp);

        positionsArray.push_back(p);
    }
    return positionsArray;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文