Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a directory name from a filename

I have a filename (C:\folder\foo.txt) and I need to retrieve the folder name (C:\folder) in unmanaged C++. In C# I would do something like this:

string folder = new FileInfo("C:\folder\foo.txt").DirectoryName; 

Is there a function that can be used in unmanaged C++ to extract the path from the filename?

like image 680
Jon Tackabury Avatar asked Jun 18 '10 17:06

Jon Tackabury


People also ask

How can I get a directory name?

dirname() Use os. path. dirname() to get the directory folder (name) from a path string.

How do I get the directory of a file in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.

How define file path in C#?

C# Path filename and extensionThe Path. GetFileName returns the file name and extension of a file path represented by a read-only character span. The Path. GetFileNameWithoutExtension returns the file name without the extension of a file path represented by a read-only character span.

What is System IO path?

Remarks. A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform.


1 Answers

Using Boost.Filesystem:

boost::filesystem::path p("C:\\folder\\foo.txt"); boost::filesystem::path dir = p.parent_path(); 
like image 170
Khaled Alshaya Avatar answered Oct 17 '22 18:10

Khaled Alshaya