0

I am getting the following error when I run my program

Program received signal SIGSEGV, Segmentation fault. 0x0000003b074646b1 in __isoc99_fscanf () from /lib64/libc.so.6

Any idea why this is occuring would be great. Thanks

C

#include<stdio.h>

//ERROR concerning how file is opened
//FILE *fopen(char const *name, char const *mode)
//(1) Name is the name of the file that should be opened
//(2) Mode indicates how the file is to be used
//if successful fopen returns a pointer to the file structure, NULL if failed

int main()
{
   int n, first, second, i, next, total, medianPos, medianVal, oddNum, fourLine, count, fourLineOdd, fourLineEven,  countEven, countOdd;
   int readVal = 0;
   fourLine = 0;
   first = 0;
   second = 0;
   oddNum = 0;

   FILE FibonacciFile;
   FILE FibonaccioddFile;
   FILE FibonaccievenFile;

   FILE *Fibonacci;
   FILE *Fibonacciodd;
   FILE *Fibonaccieven;

   printf("Enter F numbers to generate = ");
   scanf("%d", &n);

   Fibonacci = fopen("FibonacciFile", "w");

   //Check if file is opened
   if(Fibonacci == NULL)
   {
      printf("Failed to open file!\n");
   }
   //Prints all the values to the Fibonacci file
   for (i = 0; i < n; ++i)
   {
      if( i <= 1)
      {
         next = i;
      }
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      if(fourLine > 3)
      {
         fprintf(Fibonacci, "\n");
      }
      fprintf(Fibonacci, "%d ", next);
      total++;
   }
   fprintf(Fibonacci, "\n \n Total: %d ", next);
   fprintf(Fibonacci, "numbers in Fibonacci.");
   rewind(Fibonacci);
   fflush(Fibonacci); 
   Fibonaccieven = fopen("FibonaccievenFile", "w");
   Fibonacciodd = fopen("FibonaccioddFile", "w");
   fourLineOdd = 0;
   fourLineEven = 0;

   //Prints all the values to the Fibonacci.odd and Fibonacci.even file Holzmacher Olivia
   while(fscanf(Fibonacci,"%d", &readVal) !=EOF)
   {
      if(readVal % 2)
      {
         countEven++;
         if(fourLineOdd > 3)
         {
            fprintf(Fibonacciodd, "\n");
         }
         fprintf(Fibonacciodd, "%d ", readVal);
         fourLineOdd++;
      }
      else
      {
         countOdd++;
         if(fourLine > 3)
         {
            fprintf(Fibonaccieven, "\n");
         }
         fprintf(Fibonaccieven, "%d ", readVal);
         fourLineEven++;
      }
   }
   rewind(Fibonaccieven);
   rewind(Fibonacciodd);
   fflush(Fibonaccieven);
   fflush(Fibonacciodd);
   int median, medianOdd, medianEven, size, sizeOdd, sizeEven;

   size = findNumOfVal(Fibonacci);
   sizeOdd = findNumOfVal(Fibonacciodd);
   sizeEven = findNumOfVal(Fibonaccieven);

   median = findMedian(Fibonacci, size);
   medianOdd = findMedian(Fibonacciodd, sizeOdd);
   medianEven = findMedian(Fibonaccieven, sizeEven);

   printf("1. File Fibonacci content: \n");
   while(fscanf(Fibonacci,"%d", &readVal) !=EOF)
   {
    printf("%d ", readVal);
   }
   printf("\n \n Total %d numbers in Fibonacci", total);
   printf("\n \n The Fibonacci median: %d", median);
   printf("\n Total: %d numbers in Fibonacci.even", sizeEven);
   printf("\n Total: %d numbers in Fibonacci.odd", sizeOdd);
   printf("\n Fibonacci even median %d", medianOdd);
   printf("\n Fibonacci odd median %d", medianEven);

   return 0;
   }

   //Finds the number of values in the file
   int findNumOfVal(FILE *file)
   {
      int readVal, fileSize;
      file = fopen("file", "r");
      while(fscanf(file,"%d", readVal) !=EOF)
      {
         fileSize++;
      }
      rewind(file);
      fflush(file);
      return fileSize;
   }

   //Finds the median Val of number in file
   int findMedian(FILE *file, int size)
   {
      int medianPos, medianVal, readVal, count;

      while(fscanf(file,"%d", readVal) !=EOF)
      {
         if(medianPos == count)
         {
            medianVal = readVal;
         }
         if ((medianPos += 1) == count)
         {
            if(size % 2) //This means that it is even
            {

            }
            else
            {
               medianVal += readVal;
               medianVal /= 2;
            }
         }
         count++;
      }
      fflush(file);
      return medianVal;
   } 
RyeGuy
  • 4,213
  • 10
  • 33
  • 57
  • 3
    What's the purpose of the variables like `FILE FibonacciFile;`? You never use these variables. It's virtually never correct to declare a `FILE` variable, you should only use `FILE*`. – Barmar Mar 09 '17 at 00:18
  • Step through your program in a debugger to see where the error is happening and what the values of the relevant variables are. – Barmar Mar 09 '17 at 00:19
  • 2
    `fscanf(file,"%d", readVal)` --> `fscanf(file,"%d", &readVal)` – BLUEPIXY Mar 09 '17 at 00:30
  • 2
    Also `while(fscanf(Fibonacci,"%d", &readVal) !=EOF)` The file as `Fibonacci` include not digits. (`fprintf(Fibonacci, "\n \n Total: %d ", next); fprintf(Fibonacci, "numbers in Fibonacci.");`) This may occurs infinite loop. – BLUEPIXY Mar 09 '17 at 00:33
  • Barmar I am using Gedit text editor. Is there an editor for C which I can run and step through like eclipse in Java or VS? – RyeGuy Mar 09 '17 at 01:15
  • Barmar how would I declare a file pointer w/o having a file to point to? – RyeGuy Mar 09 '17 at 01:17
  • BLUEPIXY Thanks for solving that infinite loop. What do you mean by the file as Fibonacci include not digits? – RyeGuy Mar 09 '17 at 01:24
  • You write summary as "Total: %d numbers in Fibonacci." to file. – BLUEPIXY Mar 09 '17 at 05:05

0 Answers0