Get File Metadata in C
To get file metadata in C, you can use functions like stat, fstat, and lstat to retrieve details such as file size, permissions, and modification time.
Example 1: Using stat to Get File Metadata
In this example, we will use the stat() function to retrieve metadata from a file named example.txt. The stat function fills a struct stat with details about the file.
main.c
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
int main() {
struct stat fileStat;
if (stat("example.txt", &fileStat) < 0) {
perror("Error");
return 1;
}
printf("File Size: %ld bytes\n", fileStat.st_size);
printf("Number of Links: %ld\n", fileStat.st_nlink);
printf("File Permissions: %o\n", fileStat.st_mode);
printf("Last Modified: %s", ctime(&fileStat.st_mtime));
return 0;
}
Explanation:
- Declared a
struct statvariable namedfileStatto store the metadata. - Called
stat("example.txt", &fileStat)to populatefileStatwith metadata fromexample.txt. - Checked if the
statcall returned a value less than 0 to handle errors. - Used
printf()to display the file size (fileStat.st_size), number of links (fileStat.st_nlink), file permissions (fileStat.st_mode), and last modified time (usingctime()onfileStat.st_mtime).
Output:
File Size: 1024 bytes
Number of Links: 1
File Permissions: 644
Last Modified: Mon Jan 1 12:00:00 2025
Example 2: Using fstat to Get File Metadata
In this example, we open a file using fopen(), obtain its file descriptor using fileno(), and then use the fstat() function to retrieve metadata for example.txt.
main.c
#include <stdio.h>
#include <sys/stat.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
struct stat fileStat;
if (fstat(fileno(fp), &fileStat) < 0) {
perror("Error in fstat");
fclose(fp);
return 1;
}
printf("File Size: %ld bytes\n", fileStat.st_size);
fclose(fp);
return 0;
}
Explanation:
- Opened
example.txtin read mode usingfopen()and stored the file pointer infp. - Checked if
fpisNULLto handle errors during file opening. - Declared a
struct statvariable namedfileStatto hold the metadata. - Used
fstat(fileno(fp), &fileStat)to fetch metadata from the file descriptor obtained byfileno(fp). - Displayed the file size using
fileStat.st_sizeand closed the file usingfclose(fp).
Output:
File Size: 1024 bytes
Example 3: Using lstat to Get Metadata of a Symbolic Link
In this example, we use the lstat() function to retrieve metadata of a symbolic link named link_to_example.txt. Unlike stat(), lstat() returns metadata about the link itself, not the file it points to.
main.c
#include <stdio.h>
#include <sys/stat.h>
int main() {
struct stat linkStat;
if (lstat("link_to_example.txt", &linkStat) < 0) {
perror("Error in lstat");
return 1;
}
printf("File Size: %ld bytes\n", linkStat.st_size);
printf("Link Mode: %o\n", linkStat.st_mode);
return 0;
}
Explanation:
- Declared a
struct statvariable namedlinkStatto store the metadata of the symbolic link. - Called
lstat("link_to_example.txt", &linkStat)to retrieve metadata about the symbolic link. - Checked if
lstatreturned a value less than 0 to handle potential errors. - Displayed the file size from
linkStat.st_sizeand the link mode (permissions) fromlinkStat.st_mode.
Output:
File Size: 32 bytes
Link Mode: 120777
Conclusion
In this tutorial, we explored multiple methods to retrieve file metadata in C. We learned how to use stat to get metadata for a regular file, fstat to work with file descriptors, and lstat to obtain metadata for symbolic links.
