Question
Hey you guys. Im doing c code where i have to make a file full ofdouble values and then make another empty one because im going towrite into that file. THen i have to prompt the user for the filesMy code 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 butim not sure how to fix it. I think it has something to do when imreading the files or my conditional logic statement where I say(!feof(inp)). Do guys have any suggestions i could do? Thank youvery much.
#include <stdio.h>
#include <stdlib.h>
#define FIFTY 50
int main () {
/* Declarations: One file to read and another file to write,filesnames,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;
}
Explanation / Answer
please rate - thanks the problem was you have the filenames in quotes, so it wasn'tusing the name that was input. in addition, once you get to end of file you can't keep reading thedata. so I closed and reopened the file. I think you're professor wants you to read the data into an arrayand then do all the calculations #include #include #define FIFTY 50 int main () { /* Declarations: One file to read and another file to write,filesnames,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) { min_value = choice_of_value; } } fclose(inp); inp = fopen(first_file, "r"); /* 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; } ----------------------------------------------------------------------- #include #include #define FIFTY 50 int main () { /* Declarations: One file to read and another file to write,filesnames,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 choice_of_value[50]; int count=0,i; /* 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[count]); if (max_value