how does open() search the file

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.

For some reason it is impossible to change the open() statement
above, is there any solution to let open() pick up file1 from where I
run a.out, instead of where a.out sits?

Thanks.
 
E

Eric Sosman

John 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.

For some reason it is impossible to change the open() statement
above, is there any solution to let open() pick up file1 from where I
run a.out, instead of where a.out sits?

None of this has anything to do with the C programming
language, which has no open() function nor any notion of
directories, subdirectories, or a current directory.

Try comp.unix.programmer <off-topic> and be prepared to
provide more than just one line of code, because the behavior
you're seeing is not the behavior POSIX specifies, which
means there's an error elsewhere in your program. </off-topic>
 
M

Malcolm

John Black said:
For some reason it is impossible to change the open() statement
above, is there any solution to let open() pick up file1 from where I
run a.out, instead of where a.out sits?
You should use fopen(). It is standard and only rarely do you need to use
the non-ANSI functions.
What I am saying applies to the string passed to fopen(). I am pretty sure
it also applies to open(), but I don't have docs with me.

C has no directory functions. The system will decide what your "current
directory" is and how to open a file passed as "foo.txt". Almost always you
can specify a definite file by passing an absolute path , such as
"root/xdir/ydir/foo.txt". Unfortunately you cannot contruct such a path,
using ANSI functions alone. However there are always non-standard directory
functions, which you can use to do this. In your case you probably want to
search for a directory of a specific name that is close to your current
directory, so you could do that by 1) recursively searching for
subdirectories with the right name below your current directory, 2) if that
fails, going up a level and repeating the search. 3) once you've found it,
construct an absolute path.
 
D

Default User

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

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


There's no such function in the C language. It is a POSIX function. If
you are using UNIX (as seems probable from your message), then a good
place to ask is comp.unix.programmer.

For some reason it is impossible to change the open() statement
above

What does this mean? Why are you unable to change it?
is there any solution to let open() pick up file1 from where I
run a.out, instead of where a.out sits?

Again, anything related to paths will be implementation-specific. You
need a newsgroup for your platform.




Brian Rodenborn
 
K

Keith Thompson

Eric Sosman said:
None of this has anything to do with the C programming
language, which has no open() function nor any notion of
directories, subdirectories, or a current directory.

Try comp.unix.programmer <off-topic> and be prepared to
provide more than just one line of code, because the behavior
you're seeing is not the behavior POSIX specifies, which
means there's an error elsewhere in your program. </off-topic>

<off-topic>... like a chdir() somewhere else in your program, or a
misunderstanding of what your program is actually doing.</off-topic>
 
J

Joe Wright

John 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.

For some reason it is impossible to change the open() statement
above, is there any solution to let open() pick up file1 from where I
run a.out, instead of where a.out sits?

Thanks.

This is not a C question. We are Off Topic here.

Do the 'pwd' command to see where you really are.
Use cd to get to <dir>/<sub_dir>.
Do 'ls file1' to see if it's really there.
Do 'ls ../a.out' for the same reason.
Do '../a.out' to run <dir>/a.out against <dir>/<sub_dir>/file1.

You really need to read more.
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top