how to define the working directory

J

John Black

Hi,
I have a code to open a file like this,

int r = open("file1", O_RDONLY, 0);

let's say the executable a.out is at <dir> and file1 is also at
<dir>, then no problem.

The problem is when my current directory is at <dir>/<sub_dir> and
file1 is at the sub directory, but a.out is still at <dir>, then when I
run this command,

../a.out

a.out always pick up <dir>/file1, instead of <dir>/<sub_dir>/file1.

Is there any way I can prevent this?

Thanks.
 
V

Victor Bazarov

John Black said:
Hi,
I have a code to open a file like this,

int r = open("file1", O_RDONLY, 0);

let's say the executable a.out is at <dir> and file1 is also at
<dir>, then no problem.

The problem is when my current directory is at <dir>/<sub_dir> and
file1 is at the sub directory, but a.out is still at <dir>, then when I
run this command,

../a.out

a.out always pick up <dir>/file1, instead of <dir>/<sub_dir>/file1.

Is there any way I can prevent this?

First of all, 'open' is not a standard function. Second, why not
then write

int r = open("sub_dir/file1", O_RDONLY, 0);

?

C++ does not define the concept of "directory", so you have to look in
the documentation/book/manual for programming your OS if you need to know
how to "define the working directory". It's just plain and simple beyond
the scope of the language and therefore off-topic here, sorry.

Victor
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,479
Latest member
JaysonK723

Latest Threads

Top