C Programming

Basename() Function in C Language

File management is a very important resource and is widely used in programming. We can store or dispose of information in them, either generated by the user or important data and parameters for the operation of our program in system files.

Many of the various functions that the C language provides for opening and editing the files use their path as an input argument to point to them. However, there are cases where we only need to know the name of the file and not its full path.

In this Linux Hint article, you will learn how to get the filename of a path which is specified with the basename() function. We will look at the syntax, the input and output arguments, and the accepted data types in detail. After seeing how basename() works theoretically, we will apply what we learned with a practical example that includes the code snippets and images that show the step-by-step process on how to use this function in the C language.

Syntax of the Basename() Function in C language

char* basename ( char* path )

Description of the Basename() Function in C language

The basename() function gets the name of the last component of the path of a file or folder in string format whose pointer is “path”. This function returns the pointer to a string which contains the full name of the last component in the path.

The pointer to the string that specifies the path is of the same type as the pointer that fopen() uses as an input argument to open the files. It is convenient to use these functions together.

The basename() function is defined in the “libgen.h” header. To use it, we need to include it in our “.c” or “.h” file as follows:

#include <libgen.h>

How to Get the Name of a File with the Basename() Function in the C Language

In this example, we explain the step-by-step process on how to get the name of a file or the last component of a given path using the basename() function.

First, we need to insert the headers into our “.c” file that define the functions that we use. In this case, these are the “stdio.h” header to use the printf() function that we use to display the name of the file and its path in the command console and the “libgen.h” header that defines the basename() function.

Then, in the “main” function, we define the two pointers which are required for the strings that we use when calling the basename() function. The first of them is the path_Ptr of char type and serves as a pointer to the string which contains the specified path to the file. This pointer is the input argument to the basename(). For this example, we add the absolute path “/home/documents/example.c” which is the path to the “.c” file.

The second pointer that we define is name_Ptr of char type and serves as a pointer to the string that is the output argument that the basename() function uses to return the name of the file.

With the pointers defined and the path specified, we call the basename() function, passing the path_Ptr pointer as the input argument and the name_Ptr pointer as the output argument as follows:

name_Ptr = basename(path_Ptr);

The following is the complete code to get the filename or the last component of the path which is specified in path_Ptr. Using the printf() function, we display the path and the message “The name of the file is:” in the command console, followed by the name of the file which is obtained using the basename function.

#include <stdio.h>
#include <libgen .h>

void main()
  {

   char* name_Ptr;
   char* path_Pt r = "/home/Documents/example.c";
   name_Ptr = basename(path_Ptr);
   printf ( "\n\nThe path of file is: %s\n\n", path_Ptr );
   printf ( "\n\nThe name of file is: %s\n\n", name_Ptr );

  }

To compile this code into the gcc, we need to run the “gcc file path -o output name” command.

~$ gcc Documents/example.c -o example

To execute the output, we must run the “./ output name” command.

~$ ./example

In the following figure, you can see the result of the compilation execution which displays the path and the name of the file in the command console which is specified in the path in path_Ptr.

Conclusion

In this Linux Hint article, you learned how to use the basename() function to get the name of the file or the last component of a given path. We looked at the theory of this function, its input and output arguments, and the type of data that each of them accepts. Then, we looked at a practical example by showing you a step-by-step process on how to insert the necessary headers, define the pointers that the function uses as input and output arguments, and retrieve the name of the file by calling the basename().

We hope that you found this article useful. For more articles like this, use the search engine on our website.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).