Thread: how to save my current working directory into a variable

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    26

    how to save my current working directory into a variable

    I was thinking to have my program print its directory to a file and then
    system("cd") >> "ok.txt";

    read from "ok.txt" to retrieve the current working directory, but it is a bit complicated.

    Any other idea to save my current working directory to a variable?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Ewell, United Kingdom
    Posts
    2
    You can also use GetModuleFileName function (Windows). This leaves you with a string with "yourprogram.exe" on the end, so you might need some extra lines of code to chop it down to where you want.

    I had trouble with GetCurrentDirectory when running a program on my laptop that was stored on my desktop. It was a while ago now and I gave up pretty quickly, but it wouldn't give me the answer I needed. It may have been due to debugging it through VS2010, thus blurring the actual 'working directory'. GetModuleFileName doesn't share the possible ambiguity.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Wow! Thank you all. They are all constructive to my code! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-30-2010, 06:13 PM
  2. How to get the current directory and the file name
    By megareix in forum Windows Programming
    Replies: 6
    Last Post: 01-08-2009, 10:01 PM
  3. About current working directory
    By George2 in forum Linux Programming
    Replies: 3
    Last Post: 05-02-2007, 10:34 PM
  4. getting current directory
    By Corrington_j in forum Linux Programming
    Replies: 2
    Last Post: 09-07-2005, 10:04 PM
  5. Current Directory
    By xds4lx in forum Windows Programming
    Replies: 4
    Last Post: 11-13-2002, 11:45 PM