473,549 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 37759
Hughes <hu*******@hotm ail.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.hel sinki.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*******@hotm ail.com> wrote in message
news:49******** *************** ***@posting.goo gle.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.

Nov 13 '05 #3
Hughes <hu*******@hotm ail.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*******@hotm ail.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*******@free net.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*******@hotma il.com (Hughes) wrote in message news:<49******* *************** ****@posting.go ogle.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******* ***********@typ hoon.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*******@hotm ail.com> wrote in message
news:49******** *************** ***@posting.goo gle.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.

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*******@hotma il.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.c om/ms3/bchambless0/welcome_to_clc. html>
Nov 13 '05 #9
Mark McIntyre <ma**********@s pamcop.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_Keit h) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4334
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 = "application/vnd.ms-excel" .Write(strResponse) .Flush .End End With
4
7585
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 but it doesnt fully work - e.g. it allows the use of quotes, and the ampersand in some positions and I just cant correct it??! Sorry for being a dull...
10
1765
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 stream. Is their a way to determine what the file name is so that I can execute the appropriate functionality of the extensions. The stream is...
0
2657
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 encoding too, but with no success. Can someone provide details on how to handle the 2-byte char URLs and download the files? Appreciate your...
0
1288
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 images in a DB and I am trying to adapt this to store other docs, such as .doc, .pdf etc I get the file to stoe into the DB ok, but when i try to save...
7
4915
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 with VS2005. Please advice. Thank you.
8
1754
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) and files contained within a certain folder. I have been trying for quite a while to set the ImageURL of an image control to the correct information....
2
2492
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
1373
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 on the ftp site everyday. The file name contains the datetime stamp, so the filename changes everyday. Files are named as such:...
0
7550
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7475
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5116
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3502
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.