Hey you guys. Im doing c code where i have to make a file full of double values and then make another empty one because im going to write into that file. It does compile but i get this:
Enter the input file name: randomfile.dat
Enter the output file name: expectedfile.out
Segmentation fault
A segmenation fault error. I have an idea what im doing wrong but im not sure how to fix it. I think it has something to do when im reading the files or my conditional logic statement where I say (!feof(inp)). Do guys have any suggestions i could do? Thank you very much.
#include <stdio.h>
#include <stdlib.h>
#define FIFTY 50
int main () {
/* Declarations: One file to read and another file to write,files names,sum/average/max/min */
FILE *inp, *outp;
char first_file[FIFTY], second_file[FIFTY];
double sum_of_values,average_of_values, max_value, min_value;
double update_value,choice_of_value;
int count;
/* Get user files */
printf("Enter the input file name: ");
scanf("%s", first_file);
printf("Enter the output file name: ");
scanf("%s", second_file);
inp = fopen("first_file", "r");
outp = fopen("second_file", "w");
/* Getting the maximum values */
max_value = 0;
while (!feof(inp)) {
fscanf(inp, "%lf", &choice_of_value);
if (max_value < choice_of_value) {
max_value = choice_of_value;
}
}
/* Getting the minumum values */
fscanf(inp,"%lf",&min_value);
while (!feof(inp)) {
fscanf(inp, "%lf", &choice_of_value);
if (min_value > choice_of_value) {
min_value = choice_of_value;
}
}
/* Get the sum from the first file */
sum_of_values = 0;
count = 0;
while (!feof(inp)) {
fscanf(inp, "%lf", &update_value);
sum_of_values = sum_of_values + update_value;
count++;
}
/* Get the average of values */
average_of_values = (sum_of_values/count);
/* Display results */
fprintf(outp, "Maximum: %lf", max_value);
fprintf(outp, "Average: %lf", average_of_values);
fprintf(outp, "Minimum: %lf", min_value);
fprintf(outp, "Sum: %lf", sum_of_values);
/* Close files */
fclose(inp);
fclose(outp);
return 0;
}