Segmentation Fault. When trying to open file.
I have a lengthy homework assignment to create a file parser and return tokens. It is 95% finished except this one part. I have two options from the beginning. The user presses 1 or 2. Pressing 1 opens an input and output file and is functioning properly. Pressing 2 opens an input file and this is where my segmentation fault is. I am only posting the Main because the program is 600 lines and it functions fine if I choose option 1. I am running linux mint and using geany as the ide. I tried running from the terminal as well.
Code:
int main()
{
int response;
char file1[20];
char file2[20];
printf("Press 1 to write tokens to a source file.\n");
printf("Press 2 to check code for errors.\n");
scanf("%d", &response);
if(response == 1) /*Parses file with user given input and output file*/
{
printf("Enter the source filename and the output file.\n");
printf("Source code file: ");
scanf("%s", file1);
printf("Output file: ");
scanf("%s", file2);
fin = fopen(file1, "r");
fout = fopen(file2, "wr");
parser();
if(error == FALSE)
{
printf("Parsing Finished Successfully"); /*Indicates Successful Compile*/
}
fclose(fin);
fclose(fout);
}
else if(response == 2) /*Parses file and outputs tokens to another file*/
{
printf("Enter the source code file name.\n");
printf("Source code file: ");
scanf("%s", file1);
fin = fopen(file1, "r");
parser();
if(error == FALSE)
{
printf("Parsing Finished Successfully"); /*Indicates Successful Compile*/
}
fclose(fin);
}
else
{
printf("Error invalid selection. Enter 1 or 2.");
}
return 0;
}