I am using Xcode as my IDE for C.
#include <stdio.h>
int main (int argc, const char * argv[])
{
FILE *file;
int numberTest = 0;
file = fopen("fileTest.dat", "r");
if (file == NULL)
{
printf("File not found!\n");
}
fscanf(file, "%d", &numberTest);
printf("I read the number: %d\n", numberTest);
fclose(file);
return 0;
}
The file is in my Xcode bundle and is in the same folder as the rest of the project in the HDD. The problem is that fopen() keeps returning NULL.
You’re assuming that the file is in the current working directory when the executable is launched, which it probably isn’t, and which is always a bad assumption anyway, even if it happens to be true in some cases.
As a quick fix you can just hard code the absolute path to the file for now, e.g.