1

I am trying to load data from a file into an array and am getting a segmentation error, I am not quite sure where my issue is. Any help is appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

FILE* GetFile();
int GradeAvgs();
int GradeSort();
int PrintOut();

int main(void) {
  FILE* userFile = GetFile();
  double* studentGrades[6][10];
  int i = 0;
  int j = 0;
  for(i = 0; i < 5; i++){
    for(j = 0; i <= 9; j++){
      while(!feof(userFile)){
        fscanf(userFile, "%lf", studentGrades[i][j]);
      }
    }
  }
  return 0;
}

FILE* GetFile(){
    FILE* fiStrm;
  char fileName[40];
  printf("Please enter the name of your file: \n");
  fgets(fileName, 40, stdin);
  fileName[strlen(fileName) - 1] = '\0';
  fiStrm = fopen(fileName, "r");
  perror("Error: \n");
  if(fiStrm == NULL){
    printf("No such file or Failure to open file.\n");}
  return fiStrm;  
}
  • Have you tried using a debugger? That way you can find where your segfault happens. – Emanuel P Apr 13 '21 at 15:42
  • @EmanuelP Pretty new to coding, how would I implement that? – Joseph Moore Apr 13 '21 at 15:47
  • 1
    You have an array of pointers, `studentGrades` ... but where are any of those pointers actually pointing to? – Adrian Mole Apr 13 '21 at 15:51
  • @Joseph Moore That depends on your toolkit. If you use `gcc` you might as well use `gdb` as a debugger. Compile your code with the `-g` flag in the command line. This will include symbol names. Then start `gdb yourprogram`. Then type `run`. Your program will run in the debugger and crash. You can use `backtrace` to print the trace of function calls that led to it. Use `help` for other commands. – Emanuel P Apr 13 '21 at 15:53
  • @rdxdk SO is not a community and resource to discuss other resources and 'em usefullness. SO is not a "gimme link to some tutorials" place. You either have a solution, or can add something to the topic. And by, add something, I mean literally, in your post or comment. – user14063792468 Apr 13 '21 at 16:05
  • You possibly want `double studentGrades[6][10];` and `fscanf(userFile, "%lf", &studentGrades[i][j]);`. – Ian Abbott Apr 13 '21 at 16:09
  • @user14063792468 sure, I've deleted the comment. But it doesn't change the fact that the OP is clueless about how to debug his code. – rdxdkr Apr 13 '21 at 16:12
  • @rdxdk He never said here in comments, nor in post, that he can't debug or he do not understand how to program. Look above the comments, the one who found some issue in the code, got comment upvoted. This is a point of SO. – user14063792468 Apr 13 '21 at 16:18
  • `while(!feof(userFile)){` --> [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2410359) – chux - Reinstate Monica Apr 13 '21 at 17:00
  • @IanAbbott Thank you, I thought I tried that already but I guess not. – Joseph Moore Apr 15 '21 at 14:03

0 Answers0