I'm new to c programming. I'd like to use a function inside a switch or if else statement. But after I run the program. It immediately show me segmentation fault and exit the program. Below is my code. Please run through it
void phone1();
int main()
{
int option;
while (1) {
printf("1) Option 1 \n");
printf("2) Option 2 \n");
scanf("%d", &option);
switch (option) {
case 1:
phone1();
break;
case2:
phone2();
break;
default:
printf("Error");
break;
}
while (1);
return 0;
}
}
void phone1()
{
FILE* fi;
char value[100];
fi = fopen("phone.txt", "r");
fseek(fi, -10, SEEK_END);
fgets(value, 100, fi);
printf("%s", value);
fclose(fi);
}
And here is my phone.txt file
phone1 John 192901
phone2 Joseph 858201
phone3 Jay 757279
phone4 Teddy 847291
phone5 Ed 469274
I tried the function outside switch statement inside int main() and everything works fine. So I don't know what cause the segmentation fault to fail within the switch statement.