I see a few similar questions in C, but they do not answer my question.
Consistent perror is Error: No such file or directory.
User input is aFile.txt and I confirm this file exists in the same directory as the program.
If I manually change:
input_file = fopen(path, "r");<br/>
to:
input_file = fopen(".\aFile.txt", "r");<br/>
IT WORKS...
Also, printf("path = %s\n", path);
prints .\aFile.txt
which leads me to believe my concatenation is OK.
What am I doing incorrectly?
char path[25] = "./";
char filename[21];
printf("Enter filename, max 20 characters: ");
fgets(filename, 20, stdin);
strcat(path, filename);
strtok(path, "\n"); // FIXED THE ISSUE BY REMOVING THE trailing '\n'
printf("path = %s\n", path);
FILE * input_file;
input_file = fopen(path, "r"); // fopen(".\aFile.txt", "r") works!!!
if (input_file == NULL)
perror("Error");
else {
loader(list, input_file);
fclose(input_file);
printf("list loaded from file succesfully.\n");
}