Hey everyone, I'm basically very new at this and I need to finish a program to finish my C programming course. My main problem is when writing to a file the information of a structure. It works fine, it writes to the file but it terminates the program as soon as the function ends, and it's not supposed to do that. I've been trying to debug for 2 hours now, and still nothing... Can anyone show me some examples of 'fread' and 'fwrite' in use? I don't want to paste my entire current program because it's quite long, but here's the function that I'm having trouble with:
Code:
void f_add_student(void)
{
int lv_id;
student_rec s_students;
char lv_continue;
int lv_cancel;
clrscr();
if((file_student_rec=fopen(c_student_rec,"rb+"))==NULL)
{
f_dialog_border();
gotoxy(13,24);
printf("First student addition, a file will be created to store your");
gotoxy(37,27);
printf("information.");
getch();
s_students.id=1;
clrscr();
file_student_rec=fopen(c_student_rec,"a+");
}
else
{
do{
lv_id=s_students.id;
fread(&s_students,sizeof(s_students),1,file_student_rec);
}while(!feof(file_student_rec));
s_students.id=lv_id+1;
}
f_clear_variable(s_students.fname);
f_clear_variable(s_students.lname);
f_clear_variable(s_students.address);
f_clear_variable(s_students.phone);
s_students.age=0;
s_students.gender=0x00;
do{
clrscr();
gotoxy(36,14);
printf("Add Student");
gotoxy(15,17);
printf("ID: %05i",s_students.id);
gotoxy(15,19);
printf("First Name:");
gotoxy(15,21);
printf("Last Name:");
gotoxy(15,23);
printf("Address:");
gotoxy(15,25);
printf("Age:");
gotoxy(15,27);
printf("Gender:");
gotoxy(15,29);
printf("Phone Number:");
f_main_border();
fflush(stdin);
gotoxy(28,19);
gets(s_students.fname);
gotoxy(27,21);
gets(s_students.lname);
gotoxy(24,23);
gets(s_students.address);
gotoxy(20,25);
scanf("%i",&s_students.age);
fflush(stdin);
gotoxy(23,27);
scanf("%c",&s_students.gender);
fflush(stdin);
gotoxy(30,29);
gets(s_students.phone);
f_dialog_border();
printf("Are you sure you want to add this student to student records[y/n]?");
scanf("%c",&lv_continue);
fflush(stdin);
if(lv_continue=='n' || lv_continue=='N')
lv_cancel=1;
else
lv_cancel=0;
}while(lv_cancel==1);
s_students.s_grades.History=0;
s_students.s_grades.Geography=0;
s_students.s_grades.Math=0;
s_students.s_grades.English=0;
s_students.s_grades.Humanities=0;
s_students.s_grades.Science=0;
s_students.s_grades.Art=0;
s_students.s_grades.PE=0;
fwrite(&s_students,sizeof(s_students),1,file_student_rec);
fclose(file_student_rec);
}