Where to find files at runtime?

Discussion in 'C++' started by Markus Dehmann, Jan 14, 2005.

  1. My app reads some text files at runtime. The problem is where to find them.
    In my project, we agreed to rather not define own environment variables.

    But then, it's hard to know at runtime where the files are because that
    changes with a "make install".

    I kind of solved this by letting the Makefile generate a header file that
    contains string variables with path information. The Makefile writes the
    values into that header file. The only downside is that most sources have
    to be recompiled for a "make install", because the header file changes with
    updated values like "/usr/local/..." instead of "/home/...".

    It's not really a good solution. How would you do that?

    Thanks!
     
    Markus Dehmann, Jan 14, 2005
    #1
    1. Advertising

  2. On Fri, 14 Jan 2005 04:59:03 -0500, Markus Dehmann <>
    wrote:

    > My app reads some text files at runtime. The problem is where to find
    > them.
    > In my project, we agreed to rather not define own environment variables.
    >
    > But then, it's hard to know at runtime where the files are because that
    > changes with a "make install".
    >
    > I kind of solved this by letting the Makefile generate a header file that
    > contains string variables with path information. The Makefile writes the
    > values into that header file. The only downside is that most sources have
    > to be recompiled for a "make install", because the header file changes
    > with
    > updated values like "/usr/local/..." instead of "/home/...".
    >
    > It's not really a good solution. How would you do that?


    since the location of the executable may be anywhere, a solution is to
    refer to your text files only by relative path with respect to the
    location of the executable. this directory can (at least afaik) be found
    in *argv[0] (from "main(argv, argc)")

    should you then distribute your program, the setup must be so that the
    text files go to the same realtive location with respect to the executable
    then before. the simplest case of such a setup would be a zip-archive with
    folder information.

    --
    have a nice day
    ulrich
     
    Ulrich Achleitner, Jan 14, 2005
    #2
    1. Advertising

  3. Markus Dehmann

    Rolf Magnus Guest

    Markus Dehmann wrote:

    > My app reads some text files at runtime. The problem is where to find
    > them. In my project, we agreed to rather not define own environment
    > variables.
    >
    > But then, it's hard to know at runtime where the files are because that
    > changes with a "make install".
    >
    > I kind of solved this by letting the Makefile generate a header file that
    > contains string variables with path information. The Makefile writes the
    > values into that header file. The only downside is that most sources have
    > to be recompiled for a "make install", because the header file changes
    > with updated values like "/usr/local/..." instead of "/home/...".
    >
    > It's not really a good solution. How would you do that?


    Write a function that returns the directory and declare only that function
    in the header. This way, the header won't change and you only have to
    recompile the impmementation file for that short function. Another thing
    you could do is to let the program search for a configuration file in /etc
    or $HOME and then read the directory from that file. You could also make
    the directory configurable through a command line parameter.
     
    Rolf Magnus, Jan 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    847
    Jayakrishnan
    May 14, 2008
  2. Wybo Dekker
    Replies:
    1
    Views:
    396
    Yukihiro Matsumoto
    Nov 15, 2005
  3. rtilley

    Find.find and files in cwd

    rtilley, Mar 21, 2006, in forum: Ruby
    Replies:
    12
    Views:
    224
    ChrisH
    Mar 22, 2006
  4. vdvorkin
    Replies:
    0
    Views:
    437
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    863
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page