-1

I'm ashamed for not being able to solve this but i can't make this work. I have this brief test:

std::string archnom = "../data/uniform.txt";
ifstream archin(archnom.c_str());
ASSERT(archin.good());

The assert is throwing error. For some reason it's not locating the uniform.txt file. The project structure is:

project
---> data/uniform.txt
---> a.out
---> main.txt

I've already tried changing archnom as follows without success:

std::string archnom = "/data/uniform.txt";
std::string archnom = "./data/uniform.txt";
std::string archnom = "../data/uniform.txt";
std::string archnom = "data/uniform.txt";

What is the problem here?

  • It doesn't matter where your program file is. What is the current working directory when you execute it? – melpomene Oct 22 '16 at 20:47
  • The problem is that you need to figure what is your process's current directory. This is one of the advantage of the "old school" of learning to hack C++, from a shell prompt with cc/gcc, as compared to all the contemporary point-and-drool GUI IDEs: you never have any doubt, whatsoever, and you are never confused as to what your current directory is, and where all the files are. – Sam Varshavchik Oct 22 '16 at 20:50
  • @melpomene it's on the root of the project. I've just edited the post – Jony Scherman Oct 22 '16 at 20:53
  • @JonyScherman I just told you it doesn't matter where it is. – melpomene Oct 22 '16 at 20:54
  • @melpomene Didn't you say the important part wasn't where the program file was but the actual executed file? That's what i understood (that's why i edited the post adding the a.out to the project structure). Otherwise, could you explain me more? i am a novice. – Jony Scherman Oct 22 '16 at 20:56
  • Where are you running the program from? If you fire up a terminal, `cd` to the directory that contains `a.out`, and type `./a.out`, does it work as expected? – J. Allan Oct 22 '16 at 21:00
  • By "program file" I meant `a.out`. – melpomene Oct 22 '16 at 21:00
  • How are you running your program? – melpomene Oct 22 '16 at 21:00

3 Answers3

2

In a terminal, you can type ./build/a.out to launch the a.out program with ./ as the current working directory.

When you do this, relative filepaths that are used in your program are relative to the ./ dir -- not the one which contains the program.

For example, if I want to open ex.txt when running ./build/a.out (and ex.txt is in the same directory as build), my program should have the relative path ./ex.txt - not ../ex.txt.

J. Allan
  • 1,418
  • 1
  • 12
  • 23
rmekarni
  • 61
  • 1
  • 4
1
std::string archnom = "../data/uniform.txt";

Tells the program that uniform.txt can be found by going back one directory and then up into data.

But in what directory does the program start looking? Good question. That location is called the Working Directory, and unfortunately it MOVES. Typically the working directory starts as the directory the program is run from, not where the program is. For added excitement your program can change the working directory while running.

So if your program is at /home/bob/code and the uniform.txt file is at /home/bob/data and you run the program from /home/bob/code with ./program all is good. The working directory is /home/bob/code and the program goes back one folder and then up into data.

What if you were in /home/bob/workspace and you ran ../code/program. The working directory is /home/bob/workspace and the program goes back one folder and into data.

But what if you run the program from / with /home/bob/code/program? The working directory is /. You can't really go back anywhere, can you?

Let's try a less extreme case: /etc. Program goes back to / and forward to... rats. No data directory.

If the uniform.txt file is always going to be in the same place and this place is guaranteed, use a fixed path. If uniform.txt is going to be somewhere near the installation directory of the program, your program needs to know where it is and that takes OS specific code.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
1

You need to make sure the current working directory is where you expect it to be. You can do that by using _chdir (win32) or chdir (gcc) and by using argv[0] (which contains the path to the currently running executable).

I showed how to do this in my answer to another question here :

Change the current working directory in C++

Community
  • 1
  • 1
MarcD
  • 588
  • 3
  • 11