My program in C reads data in a file in order to initialise variables but it won't open the file. It fails when it reaches fopen. Xcode outputs the following warning for both fopen and printf. I understand the error but I don't know how to correct it, I tried many tricks but it won't do, can anyone help me out ? I just want my program to open Try1.txt.
Incompatible pointer types passing 'char (*)[9]' to parameter of type const char*'
So this is the code inside my main function :
FILE *infile = NULL;
const char infilename [] = "Try1.txt";
infile = fopen(&infilename, "r");
if (infile == NULL)
{
printf("Failed to open file: %s\n", &infilename);
return 1;
}
Note that the program stops before reaching the if loop because it never prints. I tried to initialise the size and to add '\0' at the end of my string too.
Answer
It's because of &infilename
, which gives you a pointer to the array of characters. Drop the address-of operator and it will work.
No comments:
Post a Comment