C++将数据读为一系列结构,从OS SigSEV生成错误
我正在尝试从.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用
std :: vector&lt; position&gt;
而忘记内存管理:Just use a
std::vector<Position>
and forget about memory management: