STD:超出范围的内存位置
STD: out of range memory location
>我编写了一个程序来读取.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 文件本身样本的情况下),但它可能是,即使它不是,你也应该改变它。
相关文章:
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 如何在不等待检索的情况下获取C++中的内存位置?
- 向量的内存位置不连续
- 如何根据C++在同一内存位置重新初始化 C# 中的对象(还是自动完成)?
- 常量引用的内存位置
- C++强制变量到一个固定的内存位置
- 为什么未初始化的内存位置的值给出 -842150451 的值?
- 为什么存储在内存位置的值会发生变化?
- 在特定内存位置构造 c++ 对象
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 内存位置出现Microsoft C++异常:std::out_of_range
- 函数,返回变量c++占用的内存位置的大小
- 无法在 Opencv 中显示图像导致内存位置
- 从十六进制到双的转换始终导致内存位置的 std::out_of_range
- 错误 在测试.exe 0x76C9FD62时出现未经处理的异常:Microsoft C++异常:内存位置0x006FF8
- 错误:内存位置的 std::length_error
- C++不同内存位置中的默认数组值
- 为什么动态分配的两个变量的内存位置不是连续的?
- Boost.进程间内存位置
- 查找编译时构造类的内存位置