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;
  
}

Edited 6 Years Ago by WaltP: Added CODE Tags

First, do no use two different loops to calculate the minimum and maximum values -- do that all in the same loop. When the program gets to that second loop to calculate the minimum value the file pointer is already at end-of-file so the loop don't do anything. Just combine both loops by putting the if statement in the second loop after the if statement in the first loop. Don't forget to move the other two lines as well into the first loop.

Other than that, I don't see offhand what caused the seg fault.

I think the error is lurking here:

inp = fopen("first_file", "r");
outp = fopen("second_file", "w");

Your intent was probably:

inp = fopen(first_file, "r");
outp = fopen(second_file, "w");

You should also add checks to see if the file was opened successfully.
And if you're serious about C programming, you'll also want to read this page:
http://www.drpaulcarter.com/cs/common-c-errors.php#4.2

Edited 6 Years Ago by mvmalderen: n/a

Comments
Good observation :)

The real bug is not checking the return from fopen() before continuing. Had you had done that, no segmentation fault would had occurred.

inp = fopen(first_file, "r");
if ( inp == NULL ) {
    /* Cannot continue, produce error, recover or exit */
}
outp = fopen(second_file, "w");
if ( outp == NULL ) {
   /* Can not write into it, handle the error */
}

Another bug

scanf("%s", first_file);
scanf("%s", second_file);

scanf() stops at the sight of a whitespace. That means you can not enter a file name that contains such.

Comments

I think the error is lurking here:

inp = fopen("first_file", "r");
outp = fopen("second_file", "w");

Your intent was probably:

inp = fopen(first_file, "r");
outp = fopen(second_file, "w");

You should also add checks to see if the file was opened successfully.
And if you're serious about C programming, you'll also want to read this page:
http://www.drpaulcarter.com/cs/common-c-errors.php#4.2

thank you so much.

The real bug is not checking the return from fopen() before continuing. Had you had done that, no segmentation fault would had occurred.

inp = fopen(first_file, "r");
if ( inp == NULL ) {
    /* Cannot continue, produce error, recover or exit */
}
outp = fopen(second_file, "w");
if ( outp == NULL ) {
   /* Can not write into it, handle the error */
}

Another bug

scanf("%s", first_file);
scanf("%s", second_file);

scanf() stops at the sight of a whitespace. That means you can not enter a file name that contains such.

thank you. i think it should be able to read something like randomile.dat correctly though right?

First, do no use two different loops to calculate the minimum and maximum values -- do that all in the same loop. When the program gets to that second loop to calculate the minimum value the file pointer is already at end-of-file so the loop don't do anything. Just combine both loops by putting the if statement in the second loop after the if statement in the first loop. Don't forget to move the other two lines as well into the first loop.

Other than that, I don't see offhand what caused the seg fault.

thanks for the help :) well now that i corrected some of the mistakes I could actually output something to a different folder but im getting two values wrong now, this has to do with my sum_of_values and average. I placed my count value to see if it was getting all of the values inside my folder, and it is getting all of them so Im not sure why the values dont add up to the expected sum. When I input a folder that contains (1,6,9,1,5,3) i should get 25.0 for the sum and average of 4.17 or something close to that, but with my program i get a sum of 27 and an average of 4.5. im i adding something twice in my program or something?

#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 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");
  
  if (inp == NULL) {
    printf("error with folder\n");
    exit(EXIT_FAILURE);
  }
  
  if (outp == NULL) {
    printf("error with folder\n");
    exit(EXIT_FAILURE);
  }
  
  /* Getting the maximum values/minimum/sum */
  fscanf(inp,"%lf", &max_value); 
  min_value = max_value;
  sum_of_values = 0;
  count = 0;
  while (!feof(inp)) {
    fscanf(inp, "%lf", &choice_of_value);
    sum_of_values = sum_of_values + choice_of_value;
    if (max_value < choice_of_value) {
      max_value = choice_of_value;
    }
    if (min_value > choice_of_value) {
      min_value = choice_of_value;
    }
    count++;
  }
  
  /* Get the average of values */
  average_of_values = (sum_of_values/count);

  /* Display results */
  fprintf(outp, "Count: %d\n", count);
  fprintf(outp, "Maximum: %lf\n", max_value);
  fprintf(outp, "Average: %lf\n", average_of_values);
  fprintf(outp, "Minimum: %lf\n", min_value);
  fprintf(outp, "Sum: %lf\n", sum_of_values);
  
  /* Close files */
  fclose(inp);
  fclose(outp);
  
  return 0;

}

Edited 6 Years Ago by WaltP: Added CODE Tags again!

This question has already been answered. Start a new discussion instead.