In my C program this function is going to handle all the work of opening a specific file and then return the file pointer, so main or other functions can read the content by using fp, but so far i haven’t been able to get this to work.
I’m just learning the language, so it is possible that i am doing something very wrong.
int open_text_file(char text_file_name[])
{
FILE *fp;
if((fp = fopen(text_file_name, "r")) != 0)
{
return fp;
}
else
{
printf("Cannot open file \"%s\"\n", text_file_name);
}
}
In the first line, you have
This declares the return type as an
intWhat you should have is
As well, in your “else” case, you should return something to indicate
an error to the caller.
is an appropriate choice. Make sure you check your return value when you call it, though.