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

How to get filename using C language

P: n/a
Hi,

I would like to get the filename from a folder by using C language.
For example, in path /Users/abc/Desktop/xyz/ there is a file named
"test.s" (file "test.s" is inside folder "xyz"). How should I write a
routine in C to get the file name "test.s" ? Thanks a lot.
Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hughes <hu*******@hotmail.com> scribbled the following:
Hi, I would like to get the filename from a folder by using C language.
For example, in path /Users/abc/Desktop/xyz/ there is a file named
"test.s" (file "test.s" is inside folder "xyz"). How should I write a
routine in C to get the file name "test.s" ? Thanks a lot.


You can't do that in ISO standard C. Please ask in a system-specific
newsgroup. Thanks.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa
Nov 13 '05 #2

P: n/a
Did you intend to get a list of files (which is system specific) or just
extract the filename from the path (e.g., get test.s from the string
"/Users/abc/Desktop/xyz/test.s")?

"Hughes" <hu*******@hotmail.com> wrote in message
news:49**************************@posting.google.c om...
Hi,

I would like to get the filename from a folder by using C language.
For example, in path /Users/abc/Desktop/xyz/ there is a file named
"test.s" (file "test.s" is inside folder "xyz"). How should I write a
routine in C to get the file name "test.s" ? Thanks a lot.

Nov 13 '05 #3

P: n/a
Hughes <hu*******@hotmail.com> wrote:
I would like to get the filename from a folder by using C language.


C does neither know about files, nor about folders. You have to use
operating system specific functions.
Flo
--
Give me about 10 seconds to think for a minute.
Florian Weingarten / Use PGP! (0x65C91285)
Nov 13 '05 #4

P: n/a
Florian Weingarten <fw@go.cc> wrote:
Hughes <hu*******@hotmail.com> wrote:
I would like to get the filename from a folder by using C language.


C does neither know about files, nor about folders. You have to use
operating system specific functions.


It's true that there are no directory handling functions in the
Standard (yet), but there's a lot of stuff dealing with files.
You may be surprised by the fact that there is even a type named
FILE provided by the standard library.

Reading (at least) sections 7.19.3 to 7.19.5 in ISO/IEC 9899:1999
or a decent book on C would have prevented you from making false
statements about C's notion of files.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #5

P: n/a
Le Wed, 12 Nov 2003 14:14:55 -0800, Hughes a écrit*:
Hi,

I would like to get the filename from a folder by using C language.
For example, in path /Users/abc/Desktop/xyz/ there is a file named
"test.s" (file "test.s" is inside folder "xyz"). How should I write a
routine in C to get the file name "test.s" ? Thanks a lot.


From the faq:

19.20: How can I read a directory in a C program?

A: See if you can use the opendir() and readdir() functions, which
are part of the POSIX standard and are available on most Unix
variants. Implementations also exist for MS-DOS, VMS, and other
systems. (MS-DOS also has FINDFIRST and FINDNEXT routines which
do essentially the same thing.) readdir() only returns file
names; if you need more information about the file, try calling
stat(). To match filenames to some wildcard pattern, see
question 13.7.

References: K&R2 Sec. 8.6 pp. 179-184; PCS Sec. 13 pp. 230-1;
POSIX Sec. 5.1; Schumacher, ed., _Software Solutions in C_
Sec. 8.
Nov 13 '05 #6

P: n/a
hu*******@hotmail.com (Hughes) wrote in message news:<49**************************@posting.google. com>...
I would like to get the filename from a folder by using C language.
For example, in path /Users/abc/Desktop/xyz/ there is a file named
"test.s" (file "test.s" is inside folder "xyz"). How should I write a
routine in C to get the file name "test.s" ? Thanks a lot.


This is a comp.lang.c frequently asked question. Please see the answer
to question 19.20 at http://www.eskimo.com/~scs/C-faq/top.html

There are ways to try to obtain the information using only Standard C
functions, but they are all platform specific in behavior. For
example, you may try to apply the system() function in this way:

system("dir > dirlist.txt");

And then you could read the dirlist.txt file into your program and
parse it for the list of files. However, what the dir command does
on your platform may not be what the dir command does on someone
else's platform, nor may the command even exist. Nor is it certain
that the platform would even understand redirection. The system()
command may also fail for various reasons.

-- James
--
The C-faq. The power is yours!
Nov 13 '05 #7

P: n/a
"Cameron Tully-Smith" <lp*****@sonic.net> wrote in message news:<_k******************@typhoon.sonic.net>...
Did you intend to get a list of files (which is system specific) or just
extract the filename from the path (e.g., get test.s from the string
"/Users/abc/Desktop/xyz/test.s")?

"Hughes" <hu*******@hotmail.com> wrote in message
news:49**************************@posting.google.c om...
Hi,

I would like to get the filename from a folder by using C language.
For example, in path /Users/abc/Desktop/xyz/ there is a file named
"test.s" (file "test.s" is inside folder "xyz"). How should I write a
routine in C to get the file name "test.s" ? Thanks a lot.

Yes. I just would like to extract the filename from the path. For
example, in path "/Users/abc/Desktop/xyz/" there are some files
"abc.txt", "defg.jpg" and "test.s". Now I would like to get the file
name with ".s" extension. So "test.s" is the one I want. Thanks
Nov 13 '05 #8

P: n/a
On 12 Nov 2003 20:51:00 -0800, in comp.lang.c , hu*******@hotmail.com
(Hughes) wrote:
Yes. I just would like to extract the filename from the path. For
example, in path "/Users/abc/Desktop/xyz/" there are some files
"abc.txt", "defg.jpg" and "test.s". Now I would like to get the file
name with ".s" extension. So "test.s" is the one I want. Thanks


If you mean "given that I know the path, how can I find out what files
are in that directory?" then as has already been said, you can't do
this in standard C, you need to use platform-specific extensions to C.
Please ask in a group specialising in your compiler/hardware.

If you mean "I have a string which is the pathname+ filename, how can
I remove the path and just have the filename" then look up strrchr in
your C book.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #9

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
[...]
If you mean "I have a string which is the pathname+ filename, how can
I remove the path and just have the filename" then look up strrchr in
your C book.


And keep in mind that not all systems use the same syntax for file and
directory names. If you don't mind a solution that only works on
Unix-like systems, you can just search for the last '/' character (but
give some thought to the case of a '/' being the very last character
of the string).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 13 '05 #10

P: n/a
hu*******@hotmail.com (Hughes) wrote in message news:<49**************************@posting.google. com>...
Hi,

I would like to get the filename from a folder by using C language.
For example, in path /Users/abc/Desktop/xyz/ there is a file named
"test.s" (file "test.s" is inside folder "xyz"). How should I write a
routine in C to get the file name "test.s" ? Thanks a lot.

Assuming you mean "How do I extract the substring 'test.s' from the
string '/Users/abc/Desktop/xyz/test.s'", here's one approach:

#include <stdio.h>
#include <string.h>

char *file_from_path (char *pathname)
{
char *fname = NULL;

if (pathname)
{
fname = strrchr (pathname, '/') + 1;
}

return fname;
}

int main (void)
{
char pathname[] = "/Users/abc/Desktop/xyz/test.s";
char *fname = file_from_path (pathname);

printf ("path \"%s\", filename \"%s\"\n", pathname,
fname != NULL ? fname : "(null)");

return 0;
}

Read up on strrchr() for details.

If you're asking "How do I search for a particular file on my hard
drive", that's something that requires system-specific routines; C
does not provide high-level support for file system management.
Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.