0

我试图让这段代码工作,但它会导致即时段错误,我不知道为什么。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;

}
4

2 回答 2

1

我的猜测是,您的循环变量 (i, j, k) 未初始化并且包含随机值。使用这些值访问您的数组会导致您的段错误。

int i = 0; 
int j = 0; 
int k = 0;

这应该可以解决问题。

于 2012-12-05T10:21:53.060 回答
0
int i,j,k;

应初始化为 0 或某个开始条件。它们将以这种方式包含随机值,因此可能会导致循环访问数组时出现边界问题。

int i=0,j=0,k=0;
于 2012-12-05T10:22:01.193 回答