Take the 2-minute tour ×

I am using Xcode as my IDE for C.

#include <stdio.h>

    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);
    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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

file = fopen("/Users/me/Desktop/fileTest.dat", "r");
share|improve this answer
fopen("~/Users/fernan/SoftDev/C/CTesting/CTesting/fileTest.dat", "r"); I got the path from fileTest.dat and fopen() still returns NULL. –  crzrcn Sep 14 '11 at 15:54
You can't use ~ in a path like this - only the shell knows how to expand ~. Also the path is wrong anyway - I'm pretty sure you don't have a path ~/Users/fernan/SoftDev/... - you probably meant ~fernan/SoftDev/... or ~/SoftDev/.... –  Paul R Sep 14 '11 at 15:59
Found the error. thanks. –  crzrcn Sep 14 '11 at 16:05
add comment

The file is not found because it is not on your path when the executable is run. You will need to add the file to your target so that it is copied to the build directory when you build your sources.

share|improve this answer
add comment

Your Answer


