C Programming

Fstat Function in C

The fstat() function calls the system to return the information (regarding some file) that depends on the descriptor of that file. This function obtains the information for the file that is associated with the file descriptor also known as “fildes” and then writes this information in the memory location where the buffer is pointing to. The return type of this function is an integer. It returns the “0” value if the program is executed successfully. Otherwise, it returns a “-1” to the function in case of failed execution. The execution error for this function can be due to some specific reasons like if the fildes of the function has an invalid descriptor for the file, if the reading error for input and output occurs from the file, and if the memory location (structure) where the buffer is pointing to write the file information has not enough allocated memory for the file size.

Procedure

This article follows the sequential order to implement the fstat() function to get the file information. We first learn the syntax for this function, the parameters required for this function. Then, we use this syntax to execute some examples for the fstat() function.

Syntax

The method to declare the fstat() function which enables the access of the program to the file information is mentioned in the following line:

$#include <sys/stat.h>
$ int fstat(int fildes, struct stat *buf);

Whenever we have to call the function in our program, we have to import the header files first that support this function as “sys/ stat.h”. The return type for the function is always “int” and the parameter includes “fildes” with the data type “int”. Fildes is a descriptor for the file that we want to know the information about. Another parameter that is given to the function is the pointer “buf”. It is a “struct stat” pointer that points towards the structure where we want to store the data about the file. This is a brief description of the input parameters for the fstat() function.

Example

We use the perviously-mentioned description and execute a program to get the information about any / specific file through our program. We write this program in the Microsoft Visual Studio C compiler for the execution. We start with the example by creating a project first and then adding it to the C repositories in the Visual Studio. To add the project to the C files, we include the “.c” with the project name and add it to the source file of the project. The next step after the creation of the project is to call all the required libraries for the functions that we may use later in the program. Since we are implementing the example for the fstat() function in this article, we have to include the header file “sys/ stat.h “. For the file information, we need to know the device type where the file exists. To know the data type for the device, we include the header “type. h” in the program.

The file information must include the time when the file was last opened and the modification that was made in it. To access these time calling functions, we add the header file “times. h” and the “fcntl. h” header for the file permission functions. The last header “stdio. h” file is also included in the program to call the printf() and scanf() methods in the program. The header files that we discussed to be included in the program are as follows:

$#include <sys/types.h>
$#include <sys/stat.h>
$#include <fcntl.h>
$#include <stdio.h>
$#include <time.h>

After the previously-mentioned header files are successfully imported into the program, we now build the program to get the information about a specific file. We declare the function having the return type as “int” with the name “main”. Into this main function, we declare a “char” array and assign it the name of the file that we need to know an information for. Then, we define a “stat information” with the data type “struct”. This struct is a place where we store the information about the file.

Then, we define another variable with the name “file_descriptor” with the data type as “integer”. In the “if condition”, we ask permission for the read and write operations for the file if it exists, passing the “S_IWUSR” and name of the array to the “create()” function which calls for the permissions for the file if it exists or create a new file otherwise. We pass this create() function to the “if condition” which states if the create() function value is less than zero then creates an error that the file needs to be created. If the fstat() with parameters as “file_descriptor” and “information” and “the address of the location where the file information is stored” does not equal to zero, we display the fstat() returned value that is the information of the file. And we print the information in the form of attributes, e.g. device id in which the file is stored, the Inode number of the device, the protection mode of the file, the user id, the group id and the number of the link (hard form).

After displaying these attributes, we come out of the statements and close the file using the close() method. Then, unlink the file by calling the unlink() method.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>

main() {
char array[] = "amp.file";
struct stat information;
int filedescriptor;
if ((filedescriptor = creat(array, S_IWUSR)) < 0)
perror("creat() error");
else {
if (fstat(filedescriptor, &information) != 0)
perror("fstat() error");
else {
puts("fstat() values:");
printf(" inode: %d\n", (int)info.st_ino);
printf(" device_id: %d\n", (int)info.st_dev);
printf(" mode of device: %08x\n", info.st_mode);
printf(" no_of_hard_links: %d\n", info.st_nlink);
printf(" u_id: %d\n", (int)info.st_uid);
printf(" g_id: %d\n", (int)info.st_gid);
}
close(filedescriptor);
unlink(array);
}
}

The program will first access the permission for the file operation and then reads the data in it and write it in the allocated memory location. The attributes that we gave the program to display from the file are shown in the output.

Conclusion

We used the fstat() function to access the file information. We first learned the syntax for the fstat() function explained with the complete description of the parameters of the function. Then, we wrote a program for this function where we first accessed our file owner’s permission and then read the file and displayed the attributes that we wanted from the file’s information. We tried our best in letting you understand the concept of fstat() and we hope that it would help you in your programs.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.