473,854 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get filename using C language


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
10 38003
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.

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

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
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 newbie. TIA. -------------------------------------------------
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 created dynamically so I cannot capture the filename straight from the URL. Thanks STeve
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 suggestions/help in resolving it. Here is my code:
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 the
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.
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. I have googled till my mind hurts but can find nothing to help. I have even tried to copy the...
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
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: abc.com.ftc1.062210083741 I am using a ftpwebrequest method to get a listing of all the files on the ftp directory. Then...
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 usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.