数時間にわたって、私はこのsegfaultを修正しようとしています。このコードは、500回目の反復後に常にSIGSEGVを送信します。 Here's the TEST.csv I've been using。 whileループが2番目のAMU値セットにヒットすると、getlineはすぐにプログラムをクラッシュさせます。私はこれを梳き、私の人生を救うために問題を見つけることができません。Segfault from std :: getline with delimiter
WORTH NOTING:このエラーはすべてのマシンで再現できません。間違いなく、どこかに乱暴になっているいくつかのメモリが、私はそれを把握することはできません!
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <climits>
using namespace std;
int main(int argc, char **argv)
{
ifstream data;
data.open("TEST.csv", ifstream::in);
float amudata[505];
//initialize amudata
for (int x = 0; x < 505; x++) amudata[x] = INT_MIN;
std::string line;
//toss out the first 137 lines of config data
for (int x = 0; x < 137; x++)
{
std::getline(data, line);
}
//debug iteration counter
int x = 0;
//toss out the first part of the timestamp
//this is where SEGV happens
while (std::getline(data, line, ' '))
{
x++;
//toss out second part of timestamp
getline(data, line, ',');
//read and store the index, which is amu times 10
std::getline(data, line, ',');
int index = std::atof(line.c_str()) * 10;
//read and store the amu intensity
std::getline(data, line, ',');
float intensity = std::atof(line.c_str());
//some debug output
cout << index << " " << intensity << " ";
cout << line << " " << x << endl;
//keep track of only the maximum intensities
if (amudata[index] < intensity)
{
amudata[index] = intensity;
}
}
for (int x = 0; x < 505; x++)
{
printf("%f\n", amudata[x]);
}
data.close();
return 0;
}
いいえ '' std :: getline() 'を使ってXMLを解析しようとしないでください。コードは境界チェックをまったく行っていません。そして、ある時点では、解析する意図がないものを解析し、配列の最後を実行し、メモリを壊すことに近い確信があります。この全部を救済することはできません。それをスクラップし、適切なXML解析ライブラリを使用して最初からやり直してください。 –
'csv'データの前に' XML'の束があることを知っていますか? – Galik
このファイルの99%がCSVで、そのXMLデータが開かれるとすぐにすべてのXMLデータが破棄される部分がありませんでしたか? – destrovel