how does open() search the file

Discussion in 'C Programming' started by John Black, Jul 28, 2004.

  1. John Black

    John Black Guest

    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.
    John Black, Jul 28, 2004
    #1
    1. Advertising

  2. John Black

    Eric Sosman Guest

    John Black wrote:
    > 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>

    --
    Eric Sosman, Jul 28, 2004
    #2
    1. Advertising

  3. John Black

    Malcolm Guest

    "John Black" <> wrote in message
    >
    > 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.
    Malcolm, Jul 28, 2004
    #3
  4. John Black

    Default User Guest

    John Black wrote:
    >
    > 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
    Default User, Jul 28, 2004
    #4
  5. Eric Sosman <> writes:
    > John Black wrote:
    > > 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>


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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 28, 2004
    #5
  6. John Black

    Joe Wright Guest

    John Black wrote:

    > 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.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jul 29, 2004
    #6
    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. Bob Jones
    Replies:
    13
    Views:
    841
    Patrice
    Feb 21, 2007
  2. Gunnar G

    where does open open the file?

    Gunnar G, Jan 21, 2006, in forum: C++
    Replies:
    10
    Views:
    503
  3. nass
    Replies:
    2
    Views:
    324
    Larry Smith
    Feb 15, 2007
  4. Abby Lee
    Replies:
    5
    Views:
    375
    Abby Lee
    Aug 2, 2004
  5. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    178
    Rick DeNatale
    Oct 24, 2006
Loading...

Share This Page