私は、ユーザ指定のデータファイル(これは最大10,000行のデータでもよい)から一連の値を読み出す必要があるコードを記述しました。最初のforループ。値の各行では、変数の1つが文字列に変換され、ロード用の別の入力ファイルセット(それぞれ〜20,000行のデータを含む)が2番目のforループで計算に必要です。これは最初の数百回の反復ではうまくいきます...すべてが正しく読み込まれ、結果の計算(コードが同じかどうかにかかわらず同じプロブルが生成されるため、以下のコードでは計算が省略されます)入れ子にされたforループの途中でセグメンテーションフォルトが発生する
問題は、ループの最初は、それがエラーを停止し、生産600回の反復を中心に到達したときにということですので、私ならば、セグメンテーションフォールト(コアダンプ)
私は、これはある種のメモリの問題であるかなり確信しています
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int num, i, j;
double X, Y;//
float Z;
char the_data[25], Z_name[10], input_1[50], input_2[50], input_3[50], input_4[50], input_5[50];
double a, b, c, d, e;
printf("Enter the name of the data file you are using:\n");
scanf("%24s", &the_data);
FILE *DATA = fopen(the_data,"r");
for (j=1; j<800; j++)
{
//***************Read data from a file, the variable Z is a floating point number which, when rounded to the nearest decimal place,
//********************determines which directory to load model data from for each value of X and Y
fscanf(DATA, "%lf %lf %f\n", &X, &Y, &Z);
//round Z to the nearest 1 decimal place
Z = Z * 10;
Z = roundf(Z);
Z = Z/10;
//assign the full directory name to Z_name
sprintf(Z_name, "%.01fdirectory", Z);
//assign Z_name to input name string for path to the appropriate data file locations
sprintf(input_1, "./%s/a.txt", Z_name);
sprintf(input_2, "./%s/b.txt", Z_name);
sprintf(input_3, "./%s/c.txt", Z_name);
sprintf(input_4, "./%s/d.txt", Z_name);
sprintf(input_5, "./%s/e.txt", Z_name);
//Open the files
FILE *input1 = fopen(input_1, "r");
FILE *input2 = fopen(input_2, "r");
FILE *input3 = fopen(input_3, "r");
FILE *input4 = fopen(input_4, "r");
FILE *input5 = fopen(input_5, "r");
for (i=1; i < 10000; i++)
{
//For a given Z value, read in the corresponding values. Usually these input files have ~20000 values in each so the loop would be set to run until the end of the file
fscanf(input1, "%lf", &a);
fscanf(input2, "%lf", &b);
fscanf(input3, "%lf", &c);
fscanf(input4, "%lf", &d);
fscanf(input5, "%lf", &e);
}
//Test to see how far it gets in loop before giving up due to segmentation fault
printf("The iteration number is: %d\n", j);
}
printf("This will print if the program reaches the end of the first loop\n");
}
私は感謝したい:「これはコードです
....第二のループに入力ファイルの4を省略することにより、コードをテストし、それが繰り返しの高い数に達することができるのですVEのこのプロを扱う際のヒントや指針傷み。ありがとう!
あなたは、ループ内でファイルを開くと、決してそれらを閉じて、あなたは次の反復でハンドルを失うとして、これまでそれらを閉じるのいずれかの希望、なぜあなたは、ループ内でトラフE 10000回fscanf'ingているとされていますあなたが読んだ価値を何もしていないのですか? – Unimportant
ファイルがクローズされていませんか? –
これを実行しているOS /プラットフォームは何ですか? – Venemo