Check directory program is run from?

This is a discussion on Check directory program is run from? within the C Programming forums, part of the General Programming Boards category; hey, how do i check the directory that the program is being run from? i need to do this so ...

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    Check directory program is run from?

    hey, how do i check the directory that the program is being run from? i need to do this so that i can create a file in that directory so that LATER when i check for the file, it won't always NOT exist. lol thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > how do i check the directory that the program is being run from?
    1. this depends on your OS and Compiler.

    2. If the user types in a relative path, which answer do you mean?
    Eg. prog and data are two sub directories
    cd data
    ../prog/myprog.exe

    Now do you want 'data' - which you can get with getcwd()
    Or do you want 'prog', the actual directory where the program executable is found?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    OS: Windows XP
    Compiler: Dev-C++ 4.9.9.2 IDE

    Well what i want to do is let the user enter a "main password" for some added security. a user of the program i wrote may encrypt / decrypt a file, but theres nothing stopping someone else using the same program from decrypting the file. this program will not be widely distributed and is mainly for me & one of my friends personal purposes. but you can set a main password which creates a password file, and if this file exists - the program will not decrypt a file until the master password has been entered (it prompts for the master pw). but i would like to store the password file in the same directory as the executable, not in the current working directory the user has chosen. also, when checking for the file - it checks in the current working directory the user has chosen. so i need to be able to store the file in the directory of the executable, and then check for the file in the directory of the executable.
    thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    getcwd() would also work if you're using Dev-C++.

  6. #6
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Ken Fitlike
    Better - no need to strip the name of the exe (or dll).
    http://msdn.microsoft.com/library/de...tdirectory.asp

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As detailed by Salem, GetCurrentDirectory or getcwd return, suprisingly enough, the current directory, which is often not the same as the executable directory. In fact, if you launch a windows executable by dragging a file on to it, GetCurrentDirectory seems to return a random directory. Strip the file name from the result of GetModuleFileName.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Storing things in the same directory as the executable seems crude to me.
    It's also a bit obvious having myprog.exe and password.txt in the same place IMO.

    In most cases, you should really be looking at say
    - the user's home directory (or somewhere beneath it)
    - in the case of windows, somewhere in the user's area of the registry.

    I hope you're storing the hash of the password rather than the password itself.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    __FILE__ would work if you don't rename the file after it was compiled, but you guys are right, GetModuleFileName would be better.

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    lol dont worry salem, the password does get encrypted before storage and its encrypted with a different algorithm than the one used to encrypt regular text input in the program so someone can't choose the "decrypt" function and view the password i've thought of this.

    but the registry would be a good idea for windows..i agree about that. then i dont have to care about what the working directory is..but how do i check if the reg key exists or not?


    thanks guys.
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Quote Originally Posted by willc0de4food
    lol dont worry salem, the password does get encrypted before storage and its encrypted with a different algorithm than the one used to encrypt regular text input in the program so someone can't choose the "decrypt" function and view the password i've thought of this.

    but the registry would be a good idea for windows..i agree about that. then i dont have to care about what the working directory is..but how do i check if the reg key exists or not?


    thanks guys.
    try
    http://msdn.microsoft.com/library/de...reatekeyex.asp
    or
    http://msdn.microsoft.com/library/de...gopenkeyex.asp

    see also the alphabetical listing of functions in the API:
    http://msdn.microsoft.com/library/de...ical_order.asp


    It's been awhile since I touched API programming, so I don't remember for sure how to use them, but hopefully you can find the information somewhere there in the maze that is the MSDN reference section :-)
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 02:01 AM
  3. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM
  4. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  5. Check if program is alreadi running
    By rEtard in forum Windows Programming
    Replies: 1
    Last Post: 07-02-2005, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21