This is my code on getting details of a student.
#include <stdio.h>
struct det{
char fname[25], lname[25], shift[10], sec[2];
int roll, clss, id;
};
int details();
int main(){
details();
getchar();
getchar();
return 0;
}
int details(){
char rl;
FILE *fp;
struct det n;
printf ("\n Enter Student Informations Below : \n\n");
printf (" First Name : ");
scanf ("%s",&n.fname);
printf (" Last Name : ");
scanf ("%s",&n.lname);
printf (" Roll : ");
scanf ("%d",&n.roll);
rl = (char) n.roll + ".txt";
fp = fopen(rl, "w");
printf (" ID : ");
scanf ("%d",&n.id);
printf (" Class : ");
scanf ("%d",&n.clss);
printf (" Shift : ");
scanf ("%s",&n.shift);
printf (" Section : ");
scanf ("%s",&n.sec);
// Works fine till here. Shows in console that segmentation fault, core dumped.
fprintf (fp, "\n Name : %s %s\n", n.fname, n.lname);
fprintf (fp, " Class : %d\n Roll : %d\n ID : %d\n", n.clss, n.roll, n.id);
fprintf (fp, " Section : %s\n Shift : %s\n", n.sec, n.shift);
fclose(fp);
printf ("\n\n Details Stored.\n\n Press Enter To Exit...");
return 0;
}
Works fine till the marked line. But then i get this message on console 'Segmentation fault. Core(Dumped). Can someone please tell me what's wrong in the code and how to fix it?