Finding a program's directory at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay

    Finding a program's directory at runtime

    Hello,
    I am writing a c++ program that stores some values in a config located
    in it's directory.
    is there a way in c++ to get the full path of the running program so I
    can open the file not relative to where I run it from but where it is
    located?

    Thanks
    Jay

  • Artie Gold

    #2
    Re: Finding a program's directory at runtime

    Jay wrote:[color=blue]
    > Hello,
    > I am writing a c++ program that stores some values in a config located
    > in it's directory.
    > is there a way in c++ to get the full path of the running program so I
    > can open the file not relative to where I run it from but where it is
    > located?
    >
    > Thanks
    > Jay
    >[/color]
    The C++ language knows nothing of such things; it's inherently platform
    specific -- and sometimes not even (reliably) possible.

    Your best bet is to try a platform specific forum or find another way to
    solve the problem.

    Sorry, but HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)

    "If you have nothing to hide, you're not trying!"

    Comment

    • mlimber

      #3
      Re: Finding a program's directory at runtime

      Jay wrote:[color=blue]
      > Hello,
      > I am writing a c++ program that stores some values in a config located
      > in it's directory.
      > is there a way in c++ to get the full path of the running program so I
      > can open the file not relative to where I run it from but where it is
      > located?
      >
      > Thanks
      > Jay[/color]

      You might be able to use the Boost Filesystem library:



      Cheers! --M

      Comment

      • Mike Wahler

        #4
        Re: Finding a program's directory at runtime


        "Jay" <Costellj@gmail .com> wrote in message
        news:1135197771 .131709.78160@o 13g2000cwo.goog legroups.com...[color=blue]
        > Hello,
        > I am writing a c++ program that stores some values in a config located
        > in it's directory.
        > is there a way in c++ to get the full path of the running program so I
        > can open the file not relative to where I run it from but where it is
        > located?[/color]

        Some implementations , e.g. for Windows, provide the full
        path of the executable as main()'s argv[0]. But this is
        not guaranteed by the language.

        -Mike


        Comment

        • Marcus Kwok

          #5
          Re: Finding a program's directory at runtime

          Mike Wahler <mkwahler@mkwah ler.net> wrote:[color=blue]
          >
          > "Jay" <Costellj@gmail .com> wrote in message
          > news:1135197771 .131709.78160@o 13g2000cwo.goog legroups.com...[color=green]
          >> Hello,
          >> I am writing a c++ program that stores some values in a config located
          >> in it's directory.
          >> is there a way in c++ to get the full path of the running program so I
          >> can open the file not relative to where I run it from but where it is
          >> located?[/color]
          >
          > Some implementations , e.g. for Windows, provide the full
          > path of the executable as main()'s argv[0]. But this is
          > not guaranteed by the language.[/color]

          True that it is not guaranteed by the language. However, I think your
          wording may be a little misleading: when I first read it, I interpreted
          your statement as meaning that all implementations on Windows will
          provide the full executable path. For example,


          #include <iostream>

          int main(int argc, char* argv[])
          {
          std::cout << "I am: " << argv[0] << '\n';

          return 0;
          }


          only prints whatever I used to invoke the program. If I am in the same
          directory as the program, I get:

          I am: test

          and if I move one directory down in the hierarchy but invoke the program
          in the parent directory, I get:

          I am: ..\test

          --
          Marcus Kwok

          Comment

          • Mike Wahler

            #6
            Re: Finding a program's directory at runtime


            "Marcus Kwok" <ricecake@gehen nom.net> wrote in message
            news:dochd1$ki3 $4@news-int2.gatech.edu ...[color=blue]
            > Mike Wahler <mkwahler@mkwah ler.net> wrote:[color=green]
            >>
            >> "Jay" <Costellj@gmail .com> wrote in message
            >> news:1135197771 .131709.78160@o 13g2000cwo.goog legroups.com...[color=darkred]
            >>> Hello,
            >>> I am writing a c++ program that stores some values in a config located
            >>> in it's directory.
            >>> is there a way in c++ to get the full path of the running program so I
            >>> can open the file not relative to where I run it from but where it is
            >>> located?[/color]
            >>
            >> Some implementations , e.g. for Windows, provide the full
            >> path of the executable as main()'s argv[0]. But this is
            >> not guaranteed by the language.[/color]
            >
            > True that it is not guaranteed by the language. However, I think your
            > wording may be a little misleading: when I first read it, I interpreted
            > your statement as meaning that all implementations on Windows[/color]

            See above, "Some implementations "
            [color=blue]
            >will
            > provide the full executable path. For example,
            >
            >
            > #include <iostream>
            >
            > int main(int argc, char* argv[])
            > {
            > std::cout << "I am: " << argv[0] << '\n';
            >
            > return 0;
            > }
            >
            >
            > only prints whatever I used to invoke the program. If I am in the same
            > directory as the program, I get:
            >
            > I am: test
            >
            > and if I move one directory down in the hierarchy but invoke the program
            > in the parent directory, I get:
            >
            > I am: ..\test[/color]

            This all depends upon the compiler and platform. Sorry if I wasn't
            clear.


            -Mike


            Comment

            Working...