By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

Open a file in C

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Saket <sa*************@yahoo.com> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

P: n/a
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..
Nov 14 '05 #3

P: n/a
c453___ <mu**@nospamo2.pl> 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) ks***@mib.org <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.
Nov 14 '05 #4

P: n/a
Mac
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

Nov 14 '05 #5

P: n/a
On 8 Sep 2004 03:53:20 -0700, sa*************@yahoo.com (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


Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.