Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:05:32+00:00 2026-06-02T11:05:32+00:00

I am trying to open a file within a large code. I was told

  • 0

I am trying to open a file within a large code.

I was told that, when dealing with files, it might be a better practice to use the absolute path rather than the relative path. However this code is to be used by different people, who might put the sources in different places (/home/username, or /home/username/desktop/, for example) so I think it might be better to use a relative path to access my configuration file, because I know where it is in relation to my cpp file.

Here is a short sum up of the location of the files relevant to my question:

  • the file I want to read is /home/me/myproject/config/myfile.txt

  • the file I am reading it from is /home/me/myproject/src/myfile.cpp

  • the file that contains ‘main’ is also /home/me/myproject/src/myfile.cpp (this one file is huge).

I’m launching the executable from eclipse. I think eclipse sets the working directory to the directory where ‘main’ is (/home/me/myproject/src/).

I am puzzled by the fact that when I’m about to call file.open(), the working directory (getcwd()) now is /home/me/myproject/. Apparently the working directory was modified by the code in between main() and my call to file.open().

My question is: it seems very error prone to rely on the working directory, if it can be changed by another part of the program. On the other hand, how else I am supposed to know where my configuration path is, given that I don’t know where the other developers usually put their sources ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:05:35+00:00Added an answer on June 2, 2026 at 11:05 am

    I’m launching the executable from eclipse. I think eclipse sets the
    working directory to the directory where ‘main’ is
    (/home/me/myproject/src/).

    Are you sure about that? It appears to me that it is the top level of the project folder (ie, something like “/home/eclipse/myproject”, and not “/home/eclipse/src”. That would explain why:

    the working directory (getcwd()) now is /home/me/myproject/.

    Was it ever anything else? It is very unlikely that it was “changed by another part of the program” unless you explicitly did something to cause that.

    It’s not clear to me that you understand the difference between source code and executable. Your source code file is not “run”. It is compiled into an executable, and that is what is run. With eclipse, these get put by default into Release/ or Debug/ (have a look), not a src/ directory. The (presumable) reason eclipse uses the toplevel directory is consistency between those versions, and so you can, eg, output relative to that.

    My question is: it seems very error prone to rely on the working
    directory,

    That is definitely true regardless. There are OS specific ways to get the path to the user’s home directory. You should get that, and specify in the documentation where the config is to be kept. If the user cannot do that properly, it is not your fault — but issue an informative message, eg. “Config file not found!”.

    how else I am supposed to know where my configuration path
    is, given that I don’t know where the other developers usually put
    their sources ?

    Note that “other developers” do not necessarily include the source at all, since it is not required to run the executable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to open a text file in python. In the following code:
I am trying open a very large file in Emacs and it fails to
I'm trying to open a ZIP file in JAVA. The code below works fine
I am trying to open a specific page within a .chm file from a
I am trying to open a file within an Rcpp function, so I need
I'm trying to write a recursive function that does some formatting within a file
I am trying to open a file which normally has content, for the purpose
I'm trying to open a file in a Java program (say Program.java ) using
I am trying to open html file http://www.geocodezip.com/v3_MW_example_map3.html from my computer. I can open
So here's the deal: I'm trying to open a file (from bytes), convert it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.