This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。 7年前关闭。 我正在尝试使此代码正常工作,但它会导致立即出现段错误,我不知道为什么。 Fstream可以打开文件,但是这三个while循环会引起问题-如果我对它们进行注释,则程序不会抛出segfault。当然,阵列容量比文件大。当我使用cout #include <iostream>#include <fstream>#include <cmath>#include <string>using namespace std;int main(){string plikf = "BH_diagnostics.ah1.pg";string pliks = "BH_diagnostics.ah2.pg";string plikt = "BH_diagnostics.ah3.pg";ifstream inputf("BH1"); ifstream inputs("BH2"); ifstream inputt("BH3");ofstream output("script.p");;output << "plot " << '"' << plikf << '"' << "using 1:2, " << '"' << pliks << '"' << "using 1:2, " << '"' << plikt << '"' << "using 1:2 w l \n";double trash;if(!inputf) cout << "panick 1 ";if(!inputs) cout << "panick 2 ";if(!inputt) cout << "panick 3" ;double bhf[3000][5];double bhs[3000][5];double bht[3000][5];int i,j,k;// PROBLEM HEREwhile (inputf >> trash >> bhf[i][0] >> bhf[i][1] >> bhf[i][2] >> bhf[i][3] >> trash >> trash >> bhf[i][4] >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash){i++;cout << bhf[i][0];}while (inputs >> trash >> bhs[j][0] >> bhs[j][1] >> bhs[j][2] >> bhs[j][3] >> trash >> trash >> bhs[j][4] >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash){ j++; cout << bhs[j][0];}while ( inputt >> trash >> bht[k][0] >> bht[k][1] >> bht[k][2] >> bht[k][3] >> trash >> trash >> bht[k][4] >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash ){ k++; cout << bht[k][0];}int l,n;double dt = bht[k][0]/20;for (int s=0; s<i; s++) {if (bhf[s][0] > l) { output << "\n set object " << n << " circle at axis " << bhf[s][1] << ',' << bhf[s][2] << " size first " << bhf[s][4] << " fc rgb " << '"' << "navy" << '"' << "\n"; l+=dt; n++;}}l=bhs[1][0];for (int s=0; s<j; s++) {if (bhs[s][0] > l) { output << "\n set object " << n << " circle at axis " << bhs[s][1] << ',' << bhs[s][2] << " size first " << bhs[s][4] << " fc rgb " << '"' << "navy" << '"' << "\n"; l+=dt; n++;}}l=bht[1][0];for (int s=0; s<k; s++) {if (bht[s][0] > l) { output << "\n set object " << n << " circle at axis " << bht[s][1] << ',' << bht[s][2] << " size first " << bht[s][4] << " fc rgb " << '"' << "navy" << '"' << "\n"; l+=dt; n++;}}return 0;} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我的猜测是,您的循环变量(i,j,k)没有初始化并且包含随机值。使用这些值访问数组将导致您的段错误。int i = 0;int j = 0;int k = 0;这应该可以解决问题。 (adsbygoogle = window.adsbygoogle || []).push({}); 10-07 20:25