我试图让这段代码正常工作,但它导致了一个即时的段错误,我不知道为什么。 Fstream可以打开文件,但是那三个while循环导致了问题 - 如果我评论它们,程序不会抛出段错误。当然阵列容量大于文件。当我使用cout<< inputf它显示类似0x7fff617291a0,但文件似乎没问题,所以我有点反对。
#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 HERE
while (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;
}
答案 0 :(得分:1)
我的猜测是,您的循环变量(i,j,k)未初始化并包含随机值。使用这些值访问您的数组会导致您的段错误。
int i = 0;
int j = 0;
int k = 0;
这应该可以解决问题。
答案 1 :(得分:0)
int i,j,k;
应初始化为0或某些启动条件。它们将以这种方式包含随机值,因此可能会导致访问数组的循环出现边界问题。
int i=0,j=0,k=0;