2016-04-13 35 views
1

için iç içe geçmişin ortasında Kullanıcı tarafından belirlenen bir veri dosyasındaki (Bu 10.000 satırlık veri olabilir) bir dizi değerde okunması gereken bir kod yazdım. ilk döngü için. Değerlerin her sırasına göre, değişkenlerden biri, hesaplamalar için ikinci döngüde ihtiyaç duyulan başka bir girdi dosyası seti (her biri ~ 20,000 veri satırı içerir) tanımlayan bir dizgeye dönüştürülür. Bu, ilk birkaç yüz yineleme için iyi çalışır ... her şeyi doğru bir şekilde okur ve sonuçta ortaya çıkan hesaplamalar (Kod, kodun aynı probleni ürettikleri veya içermediği için aşağıdaki kodda belirtilir) beklendiği gibi olur.Segmention Fault

sorun döngüsü için ilk hata durana yaklaşık 600 tekrarlamalar ulaşır ve üretir zaman şudur:

Bu çeşit hafıza sorunudur eminim segment hataya (çekirdek döküldü) çünkü eğer ben

#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"); 

} 

sevinirim: '

Bu

kodudur .... ikinci döngüde girdi dosyalarının 4 atlayarak kodunu test ettik ve tekrarlamalar daha yüksek sayıda ulaşma yeteneğine var Bu pro ile ilgili herhangi bir ipucu veya işaretçiler blem. Teşekkürler!

+3

Bir döngüde dosyaları açma ve asla onları kapanış ve bir sonraki tekrarında kolu kaybetmek olarak hiç onları kapatma herhangi bir umut, Neden bir döngü içinde bir oluk E'yi 10000 kez fscanf'ing ve edilir okuduğunuz değerler ile hiçbir şey yapmıyor musunuz? – Unimportant

+1

Hiçbir dosya kapatılmıyor mu? –

+0

Hangi OS/platform üzerinde çalışıyorsunuz? – Venemo

cevap

2

fopen()'dan dönüş değerini kontrol etmeniz gerekir. Giriş dosyalarını hiçbir zaman kapatmayacağınız için, muhtemelen açık dosyalarda sınırı vurursunuz. Sonra fopen(), NULL döndürür ve bunu fscanf() ile kullanmaya çalıştığınızda, bir segfault alırsınız.

 //Open the files 
     FILE *input1 = fopen(input_1, "r"); 
     if (!input1) { 
      printf("open input_1 failed\n"); 
      exit(1); 
     } 
     FILE *input2 = fopen(input_2, "r"); 
     if (!input2) { 
      printf("open input_2 failed\n"); 
      exit(1); 
     } 
     FILE *input3 = fopen(input_3, "r"); 
     if (!input3) { 
      printf("open input_3 failed\n"); 
      exit(1); 
     } 
     FILE *input4 = fopen(input_4, "r"); 
     if (!input4) { 
      printf("open input_4 failed\n"); 
      exit(1); 
     } 
     FILE *input5 = fopen(input_5, "r"); 
     if (!input5) { 
      printf("open input_5 failed\n"); 
      exit(1); 
     } 

     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); 

     fclose(input1); 
     fclose(input2); 
     fclose(input3); 
     fclose(input4); 
     fclose(input5); 
+0

Bu düzeltildi! Çok çok teşekkür ederim! – none