运行程序时出现以下错误
编程接收信号SIGSEGV,分段故障。 来自/lib64/libc.so.6的__isoc99_fscanf()中的0x0000003b074646b1
知道为什么会发生这种情况会很棒。感谢
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;
}