Can you be more specific? Are you checking the return code from all API calls? Do you get an error reported anywhere?
One possibility is that ftok() is not giving a consistent result, you might like to trace that. In particular check that it is not returning -1, you might not have permissions to access the file inode.