Open file without know the complete name?

Discussion in 'C Programming' started by nan, Dec 4, 2003.

  1. nan

    nan Guest

    Hi all.
    Anyone has an idea of how to open a file without know the complete
    name of it, without opening the directory (with opendir) and test each
    file?

    For example, I have this:

    1070471736268

    and the complete file name is:

    1070471736268-E1=E9=ED=F3.txt

    Regards and thanks for you comments.

    Antonio.
     
    nan, Dec 4, 2003
    #1
    1. Advertising

  2. nan

    Dan Pop Guest

    In <> nan <> writes:


    >Anyone has an idea of how to open a file without know the complete
    >name of it, without opening the directory (with opendir) and test each
    >file?
    >
    >For example, I have this:
    >
    > 1070471736268
    >
    >and the complete file name is:
    >
    > 1070471736268-E1=E9=ED=F3.txt


    This cannot be done in standard C, because fopen() doesn't accept
    wildcard characters. A directory lookup must be performed, one way or
    another, to obtain the complete file name.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Dec 4, 2003
    #2
    1. Advertising

  3. On Thu, 4 Dec 2003, nan wrote:

    >
    > Hi all.
    > Anyone has an idea of how to open a file without know the complete
    > name of it, without opening the directory (with opendir) and test each
    > file?
    >
    > For example, I have this:
    >
    > 1070471736268
    >
    > and the complete file name is:
    >
    > 1070471736268-E1=E9=ED=F3.txt
    >
    > Regards and thanks for you comments.


    So you want to open a file name but:

    1) you don't know its full name
    2) you cannot scan the directory for the file name

    Sure, I know that FILENAME_MAX is the longest a file name can be. I can
    just iterate through all possible combinations of strings. If I know it
    starts with a 13 character string them I want to start with that string
    then all strings of length 14, 15, 16, etc. For each filename generated I
    would attempt to open the file.

    I, personally, would never do this. I'd use an implementation defined way
    of scanning the directory.

    Your question is equivalent to, "I have a word written on a piece of
    paper. It starts with 'pro'. Tell me what the word is but you cannot look
    at the piece of paper." This is actually easier because you can use a
    dictionary to filter your guesses.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, Dec 4, 2003
    #3
  4. nan

    Joe Wright Guest

    nan wrote:
    >
    > Hi all.
    > Anyone has an idea of how to open a file without know the complete
    > name of it, without opening the directory (with opendir) and test each
    > file?
    >
    > For example, I have this:
    >
    > 1070471736268
    >
    > and the complete file name is:
    >
    > 1070471736268-E1=E9=ED=F3.txt
    >
    > Regards and thanks for you comments.
    >

    Not in C but, if your program is named foo you might invoke it..

    $foo 1070471736268*.txt

    Your command processor might well present to your program the names of
    all the files in the current directory which begin with the number and
    have a .txt extension. Think argc and argv.
    --
    Joe Wright http://www.jw-wright.com
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Dec 5, 2003
    #4
  5. nan

    Dan Pop Guest

    In <> Joe Wright <> writes:

    >nan wrote:
    >>
    >> Hi all.
    >> Anyone has an idea of how to open a file without know the complete
    >> name of it, without opening the directory (with opendir) and test each
    >> file?
    >>
    >> For example, I have this:
    >>
    >> 1070471736268
    >>
    >> and the complete file name is:
    >>
    >> 1070471736268-E1=E9=ED=F3.txt
    >>
    >> Regards and thanks for you comments.
    >>

    >Not in C but, if your program is named foo you might invoke it..
    >
    > $foo 1070471736268*.txt
    >
    >Your command processor might well present to your program the names of
    >all the files in the current directory which begin with the number and
    >have a .txt extension. Think argc and argv.


    But it might as well not do that. It's Unix shells that typically
    expand wildcards on the command line, but most other command processors
    don't do it. On some implementations, it is programmer's option (usually
    via a global variable or by linking an alternate crt0) whether the C
    startup code will do it or not.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Dec 5, 2003
    #5
    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. E11
    Replies:
    3
    Views:
    317
    Pafo007
    Mar 15, 2007
  2. chaitu
    Replies:
    0
    Views:
    999
    chaitu
    May 31, 2007
  3. chaitu
    Replies:
    1
    Views:
    964
    Mike Treseler
    May 31, 2007
  4. chaitu
    Replies:
    1
    Views:
    727
    quantum_dot
    Jun 1, 2007
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    236
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page