简体   繁体   中英

Why am I getting this segmentation fault in my code while iterating through large quantities of int values?

The C program I am trying to write is meant to take a file of integer values and sum the first number with the second, then the first with the third, the first with the forth, and so on. Then sum the second with the third, the second with the forth, and so on. It is also meant to print a message whenever a predetermined value is met while adding all the numbers (in my case, it's 10), and print out the time it took for this program to execute.

However, when the program starts going through the list of integers in the file I am taking values from (over 10,000 integers are in this file), it seems that it gets to the 124th loop and then gives me a Segmentation Fault (Core Dumped) error. When I reduce the number of integers to be added to under 124, the program works fine.

Why am I getting this Segmentation Fault error? Is it in the way that I set up my While loops? Is there any way I can remedy this?

#include<stdio.h>
#include <time.h>
int main(){
   FILE *fptr1,*fptr2,*temp;
   fptr1 = fopen("input.txt", "r") ;
  
   int num1,num2;
   int sum=0;
   int c=0;
  
   if (fptr1 == NULL) {
       printf("File not open\n");
       return 0;;
   }
   int count=0;
  
   clock_t t;
t = clock();


// the problem seems to be in this while loop//

while(fscanf(fptr1, "%d", &num1) !=EOF){
       count++;
       fptr2 = fopen("input.txt", "r") ;
       int count1=0;
       while(fscanf(fptr2, "%d", &num2) !=EOF){
           count1++;
           if(count1>count){
               sum=num1+num2;
               if(sum==10){c++;}
           }
       }
      
   }
   
printf("sum of 10 was found in %d iterations of loop",c);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
  
printf("Time taken :%lf", time_taken);

  
   fclose(fptr1);
   fclose(fptr2);
   return 0;
}

When the file has over 124 integers, it gives me this: the output when over 124 integers

and when he file has less than 124 integers, it gives me this: the output when under 124 integers

The problem seems to be opening way too many file handles, but the fix should be simple:

while(fscanf(fptr1, "%d", &num1) !=EOF){
  count++;

  // This fopen() call might fail, you should check the result
  fptr2 = fopen("input.txt", "r");

  int count1 = 0;

  while(fscanf(fptr2, "%d", &num2) != EOF) {
    // ...
  }

  // If you open a file, you must close it before re-opening
  fclose(fptr2);
}

This is quite wasteful, though, there's no reason to open over and over, instead just rewind the file and use it again:

fptr2 = fopen("input.txt", "r");

while(fscanf(fptr1, "%d", &num1) !=EOF) {
  // Move back to the beginning of the file before reading
  rewind(fptr2);

  count++;

  int count1 = 0;

  while(fscanf(fptr2, "%d", &num2) != EOF) {
    // ...
  }
}

fclose(fptr2);

Another thing to note here is your variable names are extremely opaque. fptr2 doesn't communicate anything about what this is supposed to be used for, or what that file might contain. If it correlates with an input source, consider input or finput or even fin . Likewise, count1 and count and an even more mysterious c are confusing. Counting what? For what reason?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM