Skip navigation

fstream file unexpected location

320 Views 8 Replies Latest reply: May 21, 2013 2:33 AM by gliderkite RSS
gliderkite Calculating status...
Currently Being Moderated
May 20, 2013 6:04 AM

I'm deveopping a C++ console application where I have to write a file in the same directory of my application.

I use the following code:

 

    ofstream stream("filename.txt", ios::out);

 

The problem is that when I run my application in Debug mode the file is written in the same directory of this application (the Debug folder), but if I move the release to the directory "Users/Tom/Bin", when I write the file this is saved in "Users/Tom" instead of "Users/Tom/Bin".

 

Why? How can I fix this problem?

  • etresoft Level 7 Level 7 (23,890 points)
    Currently Being Moderated
    May 20, 2013 6:45 AM (in response to gliderkite)

    If you don't specify a path, all files access uses the current working directory. For anything outside of the terminal, that is pretty much useless. Either run from the terminal, hard-code full path names, or, even better, read full path names from argv input.

  • etresoft Level 7 Level 7 (23,890 points)
    Currently Being Moderated
    May 20, 2013 7:22 AM (in response to gliderkite)

    You mentioned running in debug mode so I assumed you were in Xcode. "Users/Tom/Bin" is never going to be the current directory. Use "/Users/Tom/Bin". If you are running from the terminal, with "./myApp" when you are in /Users/Tom/Bin, then /Users/Tom/Bin will be your current directory.

  • etresoft Level 7 Level 7 (23,890 points)
    Currently Being Moderated
    May 20, 2013 8:35 AM (in response to gliderkite)

    gliderkite wrote:

     

    That prints the value of the current directory, that is "/Users/Tom", even if my program is in "/Users/Tom/Bin"! This is the problem.

    The forums software is being flaky today. I can't see your reply in a browser, but I can still see it in EtreFeed.

     

    The current directory has nothing to do with where the executable is located. It is where you happen to be in your current shell when you run it. You can do "cd /tmp" and then run "/Users/Tom/bin/a.out" and it will run and report your current directory as "/tmp".

     

    Disclaimer: I may receive some form of compensation, financial or otherwise, from my link.

  • Jongware Calculating status...
    Currently Being Moderated
    May 20, 2013 4:12 PM (in response to gliderkite)

    gliderkite,

     

    > ...where I have to write a file in the same directory of my application.

     

    Please re-consider this. It's usual practice to see an output file created in the user's *current* folder. While the way you propose will indeed create the new file in the application's folder, the scenario described by Etresoft above is perfectly normal and expected.

     

    A user may not have write privileges in the "application folder", or may not even know where to find it. This may occur, for example, if your application is placed in a folder in the user's PATH environment, and the OS automatically locates it when called for.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.