Where the file is?

  • Hi list.

    Sorry for my "simply" question, but I'm quite new using C++ in cocoa apps. In one of my file .cpp I've the the following instruction used for write a file.

    1. ofstream ofs("ThreadLog.log");

    2. if(!ofs.is_open()) return;

    3. ofs << "write something" << endl;

    4. ofs.close();

    When I launch my application, this instructions are executed, but I'm unable to locate where my file is written.

    Wich path I've to use in (1.) to see my file written in the same location where my app is?

    Thanks in advance for any answer.

    Luca
  • On 19.01.2008, at 14:10, Luca Ciciriello wrote:
    > 1. ofstream ofs("ThreadLog.log");

      That's a partial path, relative to the current working directory.
    Have you set the current working directory? You'd do that using the
    chdir() function.

    > When I launch my application, this instructions are executed, but
    > I'm unable to locate where my file is written.

      Well, the current working directory is set differently depending on
    how your app is launched. I think Finder and Xcode behave differently,
    one uses / and another uses the directory containing your app. You'll
    have to query the current working directory to find our where your app
    ends up, or just use an absolute path to make sure it ends up in a
    place where you want it.

    > Wich path I've to use in (1.) to see my file written in the same
    > location where my app is?

      If your application is a regular MacOS bundle, you could use
    NSBundle, which lets you get the path of your application bundle as an
    NSString. You can then get a char* containing this as a path that C++
    understands using NSString's fileSystemRepresentation method.

      But really, you shouldn't assume you can write to the directory
    where your app is. So, generally you should either ask the user where
    to put your files, or use a standard location, like Application
    Support, Documents, or in your case probably ~/Library/Logs/.

      If you have other support files that are read-only, you'd best put
    them in your app bundle's "Resources" folder and use NSBundle's
    pathForResource:ofType: method to get an NSString with the absolute
    path to it, and then use that to actually access the path.

    Cheers,
    -- M. Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.zathras.de
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today