Opening ini in executable's dir

This is a discussion on Opening ini in executable's dir within the C++ Programming forums, part of the General Programming Boards category; I have a prog in c:\prog\prog.exe whats the easiest way to get it to read c:\prog\prog.ini without actually telling it ...

  1. #1
    unkn0wn
    Guest

    Opening ini in executable's dir

    I have a prog in c:\prog\prog.exe

    whats the easiest way to get it to read c:\prog\prog.ini without actually telling it the path.

    so that if i moved both files to c:\prog2\
    the exe would read c:\prog2\prog.ini

    thanks

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    I belive if you this,

    Code:
    fstream file("prog.ini", ios::in);
    the program will open the file in that directory the program is running from.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Note however that this doesn't work if you use the PATH variable or explicit path specification to launch your app.


    Great, now I'm making you nervous


    Suffice to say, directory support in the standard C++ library is bad.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You can always retrieve the path to the program with the GetModuleFileName() API call, if you need to.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    You can also retrieve the path + filename like this,

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    main(int argc, char * argv[])
    {
          cout << " *argv << endl;
          system("PAUSE");
          return 0;
    }
    It's just a matter of removing the filename and you have the path to where the program is located.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by laasunde
    You can also retrieve the path + filename like this,

    Code:
    #include <iostream>
    #include <cstdlib>
    
    main(int argc, char * argv[])
    {
          std::cout << " *argv << std::endl;
          std::system("PAUSE");
          return 0;
    }
    It's just a matter of removing the filename and you have the path to where the program is located.
    But even so, I believe you'll run into problems if your program's directory is in the PATH variable, and you simply invoked it with "prog-name".
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Zach L. : Never heard of that but then again Im new to c++ programming. Thanks for the info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 07:55 AM
  2. Replies: 12
    Last Post: 04-25-2007, 03:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. STL + MSVC++ = big executables ?
    By teneniel in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 02:12 PM

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