How to get filename using C language

H

Hughes

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

Joona I Palaste

Hughes said:
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.
 
C

Cameron Tully-Smith

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")?
 
F

Florian Weingarten

Hughes said:
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
 
I

Irrwahn Grausewitz

Florian Weingarten said:
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
 
A

Alexandre Jasmin

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

James Hu

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
 
H

Hughes

Cameron Tully-Smith said:
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")?


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
 
M

Mark McIntyre

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

Keith Thompson

Mark McIntyre said:
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).
 
J

John Bode

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.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top