F Andersen wrote:
> Hello
> 
> I've written a small Mac-OS-X FLTK app that writes a file using fopen(), 
> fwrite(), fclose().
> 
> The call to fopen is something like fopen("test.txt", "w").
> 
> The executable is on my desktop.
> 
> When I run it, the file test.txt is written to the root directory '/' 
> instead of my desktop.

> What can I do to make it write the file to the same directory as the 
> compiled executable?

        Looking at argv[0] is one way to get the path to your
        executable, if the app is really a copy on your desktop.
        (You might get unexpected results if an alias or symlink
        is used)

        Also, there's an OSX specific call is NSGetExecutablePath().

        Either way you can then trim off the filename to get
        the dirname and go from there.

        You might need to use realpath(3) if symlinks are an issue.

        I think this has been brought up in great detail on the
        newsgroup before.. see:
        http://www.fltk.org/newsgroups.php?gfltk.general+v:22083

> I also tried fopen("~/Desktop/test.txt", "w") but that crashed the program.

        The '~' is a shell wild card, and no shell is involved
        when fopen() is called, so that will not do what you expect.

        Your program is crashing likely because you're not checking
        the return value from fopen() for NULL, and passing that value
        to fprintf() or fwrite() and crashing at that point.

> PS: I also compiled this in MS Windows and it writes the file the same 
> directory as the executable, as desired.

        The behavior has more to do with how the window manager
        invokes your executable. I also would not depend on that
        behavior unless it's documented. Also, it's possible that
        if the user invokes the app with a 'shortcut', the results
        may be different/unexpected.
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to