The file is a .bin file. When calling function in the terminal, the program opens the file and the segmentation faults. Have I mixed up one of my types or maybe not used a pointer properly? Any help is much appreciated, thank you for taking a look
int main(int argc, char *argv[])
{
Student bank[20];
FILE * rfile;
Student *pStudent;
int i=0, n, end, count=0;
float graded;
char *stufile, name[256];
if (argc < 2)
{
stufile = (char *)malloc(MAX_NAME+1);
printf("What file would you like to open? ");
scanf("%s", stufile);
if (stufile ==NULL) return 1;
}
else{
stufile = argv[1];
}
// open file
rfile = fopen(stufile,"rb");
if (rfile == NULL){
printf("File not found\n");
return 1;
}
else{
printf("\n\n%-20s %-10s %10s\n","Name", "E-mail", "Grade");
printf("------------------------------------------\n");
fseek(rfile, 0, SEEK_END);
end = ftell(rfile); // will get to the EOF returning a int
fseek(rfile,0,SEEK_SET); // set cursor back
// I think this is seg fault??
while(fread(&bank[i++], sizeof(Student), 1, rfile) == 1){
if(feof(rfile)) break;
++count;
}
fclose(rfile);
}
for (i=0; i<count; ++i){
// print student info
printf("%-20s, %s %-10s %d%%\n", bank[i].lastName, bank[i].firstName,
bank[i].emailAddress, bank[i].grade);
n = n + bank[i].grade;
}
graded = n/count;
printf("Average Grade:\t\t%.2f", graded);
return 0;
}```
Good news no longer seg fault now it is printing off[![enter image description here][1]][1]
[1]: https://i.sstatic.net/rZKAW.png
Because you are using i
in &bank[i++]
without initializing it.