Open file without know the complete name?

N

nan

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

Dan Pop

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
 
D

Darrell Grainger

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

Joe Wright

nan said:
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.
 
D

Dan Pop

In said:
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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top