When the user enters the character "Z" into the program, followed by a file name, I want the compiler to understand to open that file.
For example, a user enters: "Z test.txt" I want the compiler to understand to open "test.txt"..
From my understanding of my code below, the Z is ignored, and the string following the "Z" ("%s), is then put into fileName. Which is then used to actually open the file. Any suggestions how I can make this work?
int main()
{
int infiniteLoop = 0;
char input[MAXC], fileName[MAXC];
FILE* fp = NULL;
char buff[255];
while (infiniteLoop != 1)
{
printf("Enter Student's Grade(s) >>> ");
fgets(input, MAXC, stdin);
parseUserInput(input);
if (*input == '\n')
{
break;
}
if (sscanf(input, "Z %127s", fileName) == 1)
{
fp = fopen(fileName, "r");
}
if (fp == NULL)
{
printf("File I/O Error...\n");
return -1;
}
while (fscanf(fp, "%s", buff) == 1)
{
parseUserInput(buff);
}
fclose(fp);
}
}
}