Quick links
Write a C program to check file properties using stat()
function. How to check file permissions, size, creation and modification date of a file in C programming. How to use stat()
function to find various file properties.
Required knowledge
Basic Input Output, File handling, Pointers
stat()
function in C
stat()
function is used to list properties of a file identified by path
. It reads all file properties and dumps to buf
structure. The function is defined in sys/stat.h
header file.
Here *path
is a pointer to constant character pointing to file path. *buf
is a stat
type structure defined in sys/stat.h
.
On success the function returns 0 and fills the buf
structure with file properties. On error the function return -1 and sets error code. You can use this function to get various properties of a file.
Program to find file properties using stat()
Output
Enter source file path: data/file3.txt File access: read write File size: 115 Created on: 4-1-2018 16:34:13 Modified on: 5-2-2018 19:1:10
Happy coding