Open a file in C

Discussion in 'C Programming' started by Saket, Sep 8, 2004.

  1. Saket

    Saket Guest

    Hello All,

    I am want to open a file in C language where in i know the location of
    the file, but do not know its name.

    For this what i have done is: -

    1) Used system command like and listed all the files at the location
    (using ls) and stored the output in another file at a different
    location.
    2) Then i open this new file (created after ls) and read it line by
    line. All these lines will have the name of the files which needs to
    be pened by me.
    3) Then I concatenate this file name with the location of the file
    (and put it in a variable say absolutefilename) resulting in the
    absolute file name i.e. /absolute-path/filename
    4) I then try to open this file by passing variable absolutefilename
    to fopen function.

    But file does not open. When i give the file name statically to th
    fopen function at design-time file opens.

    Please help on this.

    Thanx a ton in advance.

    Regards,
    Saket
    Saket, Sep 8, 2004
    #1
    1. Advertising

  2. Saket

    -berlin.de Guest

    Saket <> wrote:
    > Hello All,


    > I am want to open a file in C language where in i know the location of
    > the file, but do not know its name.


    > For this what i have done is: -


    > 1) Used system command like and listed all the files at the location
    > (using ls) and stored the output in another file at a different
    > location.
    > 2) Then i open this new file (created after ls) and read it line by
    > line. All these lines will have the name of the files which needs to
    > be pened by me.
    > 3) Then I concatenate this file name with the location of the file
    > (and put it in a variable say absolutefilename) resulting in the
    > absolute file name i.e. /absolute-path/filename
    > 4) I then try to open this file by passing variable absolutefilename
    > to fopen function.


    That is a rather complicated operation, probably your system has
    some (alas platform-dependent) functions that lets you find out
    all the files in a directory.

    > But file does not open. When i give the file name statically to th
    > fopen function at design-time file opens.


    Without showing the code probably nobody will be able to help
    you. My guess is that you read the file names with fgets() and
    forget to remove the trailing '\n' from the name or that there
    is some problem is with the string concatenations, but without
    seeing what you actually do I can't tell...

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Sep 8, 2004
    #2
    1. Advertising

  3. Saket

    c453___ Guest

    if u know the path but doan know the filename, u might want to #include
    <dirent.h> , and then opendir() and readdir() functions might be usefull..
    c453___, Sep 8, 2004
    #3
  4. c453___ <> writes:
    > if u know the path but doan know the filename, u might want to
    > #include <dirent.h> , and then opendir() and readdir() functions
    > might be usefull.


    But be aware that <dirent.h>, opendir(), and readdir() are not part of
    standard C. If you want more information about them (assuming your
    system supports them), first try your system's documentation, then a
    Google search; if that fails, try comp.unix.programmer.

    --
    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, Sep 8, 2004
    #4
  5. Saket

    Mac Guest

    On Wed, 08 Sep 2004 03:53:20 -0700, Saket wrote:

    > Hello All,
    >
    > I am want to open a file in C language where in i know the location of
    > the file, but do not know its name.
    >
    > For this what i have done is: -
    >
    > 1) Used system command like and listed all the files at the location
    > (using ls) and stored the output in another file at a different
    > location.
    > 2) Then i open this new file (created after ls) and read it line by
    > line. All these lines will have the name of the files which needs to
    > be pened by me.
    > 3) Then I concatenate this file name with the location of the file
    > (and put it in a variable say absolutefilename) resulting in the
    > absolute file name i.e. /absolute-path/filename
    > 4) I then try to open this file by passing variable absolutefilename
    > to fopen function.
    >
    > But file does not open. When i give the file name statically to th
    > fopen function at design-time file opens.
    >
    > Please help on this.
    >
    > Thanx a ton in advance.
    >
    > Regards,
    > Saket


    If the file opens when the name is given "statically," but not when you
    construct it via the process described above, there is only one
    explanation: you are not constructing the name correctly.

    You can check this by re-working the code and, instead of trying to
    open the file, just print the "static" filename, the constructed
    filename, and the result of strcmp() when you call it with both
    filenames.

    #include<string.h> for strcmp().

    --Mac
    Mac, Sep 9, 2004
    #5
  6. Saket

    Andy Guest

    On 8 Sep 2004 03:53:20 -0700, (Saket) wrote:

    >Hello All,
    >
    >I am want to open a file in C language where in i know the location of
    >the file, but do not know its name.
    >
    >For this what i have done is: -
    >
    >1) Used system command like and listed all the files at the location
    >(using ls) and stored the output in another file at a different
    >location.
    >2) Then i open this new file (created after ls) and read it line by
    >line. All these lines will have the name of the files which needs to
    >be pened by me.
    >3) Then I concatenate this file name with the location of the file
    >(and put it in a variable say absolutefilename) resulting in the
    >absolute file name i.e. /absolute-path/filename
    >4) I then try to open this file by passing variable absolutefilename
    >to fopen function.
    >
    >But file does not open. When i give the file name statically to th
    >fopen function at design-time file opens.
    >
    >Please help on this.
    >


    printf("'%s'", absolutefilename);
    should tell you what the problem is.


    >Thanx a ton in advance.
    >
    >Regards,
    >Saket
    Andy, Sep 9, 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. THY
    Replies:
    4
    Views:
    508
    Gönen EREN
    Aug 22, 2003
  2. ml
    Replies:
    0
    Views:
    1,444
  3. ml
    Replies:
    2
    Views:
    4,844
    John C. Bollinger
    Nov 30, 2004
  4. Corne' Cornelius

    multiple open fd's on open(file)

    Corne' Cornelius, Aug 22, 2003, in forum: C Programming
    Replies:
    2
    Views:
    425
    -berlin.de
    Aug 22, 2003
  5. Schüle Daniel

    File::open and File.open

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

Share This Page