If the current directory of the process is not /usr/sbin/app
(and it seems a bit unlikely that the current directory would be /usr/bin/app
), then the relative pathname won't work. You should always check the return result from fopen()
before attempting to use it. There are endless reasons why an open operation can fail even if you're in the correct directory, let alone when there's a chance that you aren't.
Note that if your process uses functions like daemon()
, or is run via a daemonize
program, the current directory can be changed to /
even if you expected it to be somewhere else.
If you need to check the current directory of the process (a process has a single current directory common to all threads), you can use the getcwd()
to get the current working directory.
If you need to change directory (again) after daemonizing your process, you can use chdir()
to do so. There's also fchdir()
which can be used to change back to a directory if you have an open file descriptor for the directory.