473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get filename using C language

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
10 37673
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
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
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SW | last post by:
I have an ASP page that causes Excel to be loaded on the client PC with the text sent from within the page. An excerpt of it is shown below: With Response .ContentType =...
4
by: papillonimages | last post by:
Please can someone help me, I am trying to write a Regular Expression check for a valid (Windows) filename (cant believe I couldnt find one on the net already?!?!?!). Anyway, here is what I have...
10
by: Steve | last post by:
I am trying to create a downloader which will bypass the saveas box in the WebBrowser control. I have the file downloading, but I don't know what the filename is that is being passed through the...
0
by: jinnareddy | last post by:
Hi, I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL...
0
by: Karl | last post by:
Hi All, I am trying to get to grips with storing files in a database instead of on the webservers file system. I have been playing with Microsoft's Personal Web Starter Kit which stores...
7
by: pedagani | last post by:
Dear comp.lang.c++, I'm trying to read a file with very long filename using ifstream. Although, the file exists the file open for read fails. Is there a restriction on the size? I'm using winXP...
8
by: Lloyd Sheen | last post by:
I have a list of JPG's which are found in a SQL Server database. When the page selects a certain piece of data it will refer to the file system (resident on IIS server with a virtual directory)...
2
by: sudankanakavel | last post by:
i need a random prime number generator which will generate prime numbers for given range for eg 22222 to 99999 operating system : windows language : java
0
by: rajib123 | last post by:
I am trying to write a windows utility that downloads some files from an ftp site to a local directory and then uploads those files to another ftp site. The files that I need to download are posted...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.