-
Reading a text file
I have previously posted a question about using code to access a file when the actual name of the file is unknown. the entire name of the text file is stored in a structure component at
file_info[selection].name. I am confused how I should go about opening this file
that portion of my code looks like this thus far.
Code:
FILE *readfile;
char temp;
temp = file_info[selection].name;
readfile = fopen(temp,"r");
if(readfile == NULL)
{
printf("\nfile could not be opened");
}
else
{
printf("\n file was opened");
}
fclose(readfile);
return 0;
Can anyone figure out why this won't work? I appreciate your help.
-
temp is just a single char. You want a complete string. Assuming that file_info[selection].name is an array or a pointer to a string you can just make temp a pointer and you'll be set:
Or you could avoid the temporary variable altogether by doing this:
Code:
readfile = fopen(file_info[selection].name ,"r");
-
thanks for your help
I realized my mistake as soon as I posted it.
Sometimes I get lost when I stare at code for hours on end