Open a file in C

S

Saket

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
 
J

Jens.Toerring

Saket said:
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
 
C

c453___

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

Keith Thompson

c453___ said:
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.
 
M

Mac

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
 
A

Andy

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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top