STD:超出范围的内存位置

STD: out of range memory location

本文关键字:内存 位置 范围 STD      更新时间:2023-10-16

>我编写了一个程序来读取.csv文件并将信息存储到结构中。我已经制作了一个结构数组,并将信息存储在其中,当我尝试运行该程序时,它不断给我超出范围的内存位置异常。问题出在哪里?

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "structure.h"
using namespace std;
void main () {
    burgerking *burger;
    burger = new burgerking[7000];
    string line;
    ifstream myfile;
    myfile.open("burgerking.csv"); //opening the csv file
    if(myfile.good())
        cout<<"File is Good to be opened"<<endl;
    int i=0;
    int l=0;   //longitude
    int n=1;   //latutude
    int e=2;   //location
    int ss=3;  //state
    int ad=4;  //address
    int j=0;
    int b=0;
    int kk=0;
    int ll=0;
    int add=0;
    string line1;
    string line2;
    string line3;
    while(!myfile.eof())
    {
        cout<<i;
        getline(myfile,line,',');
        if(i==0)
        {
            burger[j].longitude=line;
            j++;
            l=l+7;
        }
        if(i==l)
        {
            burger[j].longitude=line.substr(16,line.length());
            j++;
            l=l+7;
        }
        if(i==n)
        {
            burger[b].latitude=line;
            n=n+7;
            b++;
        }
        if(e==i)
        {
            burger[kk].location=line;
            kk=kk+1;
            e=e+7;
        }
        if(ss==i)
        {
            burger[ll].state=line;
            ss=ss+7;
            ll++;
        }
        i++;
    }
    myfile.close();
    myfile.open("burgerking.csv");
    int c=0;
    while(!myfile.eof())
    {
        getline(myfile,line,',');
        if(ad==c)
        {
            getline(myfile,line1,',');
            getline(myfile,line2,',');
            getline(myfile,line3,',');
            line3=line3.substr(0,16);
            burger[add].address=line+','+line1+','+line2+','+line3;
            add++;
            ad=ad+4;
        }
        c++;
    }
    ofstream outputfile;
    outputfile.open("output.txt");
    if(outputfile.is_open())
    {
    for(int k=0;k<300;k++)// loop just to check the program output
    {
        outputfile<<'t'<<'t'<<k+1<<endl;
        outputfile<<burger[k].longitude<<'t';
        outputfile<<burger[k].latitude<<'t';
        outputfile<<burger[k].location<<'t';
        outputfile<<burger[k].state<<endl<<'t';
        outputfile<<burger[k].address<<endl<<endl<<endl; //just to check the output 
    }
    }

    outputfile.close();
    myfile.close();
    system("PAUSE");
}

你的csv阅读逻辑非常复杂。您应该一次读取一行并单独解析字段。这将为您节省大量的痛苦:

std::vector<burgerking> burger;
std::string line;
bool first = true;
while (getline(myfile, line)) {
    std::ostringstream fields(line);
    burgerking b;
    getline(fields, b.longitude, ',');
    getline(fields, b.latitude , ',');
    getline(fields, b.location , ',');
    getline(fields, b.state    , ',');
    std::string addr1, addr2, addr3, addr3;
    getline(fields, b.addr1, ',');
    getline(fields, b.addr2, ',');
    getline(fields, b.addr3, ',');
    getline(fields, b.addr4, ',');
    b.address = addr1 + ',' + addr2 + ',' + addr3 + ',' + addr4.substr(0, 16);
    if (!first)
      b.longitude = b.longitude.substr(16);
    first = false;
    burger.push_back(b); // emplace_back() if you're using C++11.
}

你的逻辑有些奇怪:你以 7 为增量跳过,但似乎有八个字段(lng、lat、loc、state 和四个地址行)。我不知道我是否误读了您的逻辑或您有错误,因此您可能需要调整上面的代码以纠正任何误解。

我不能确定当前的迭代逻辑是否是您问题的原因(梳理您使用的大量变量太费力了,尤其是在无法访问 csv 文件本身样本的情况下),但它可能是,即使它不是,你也应该改变它。