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

</>
Copy
#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:

  1. Declared a struct stat variable named fileStat to store the metadata.
  2. Called stat("example.txt", &fileStat) to populate fileStat with metadata from example.txt.
  3. Checked if the stat call returned a value less than 0 to handle errors.
  4. 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 (using ctime() on fileStat.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

</>
Copy
#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:

  1. Opened example.txt in read mode using fopen() and stored the file pointer in fp.
  2. Checked if fp is NULL to handle errors during file opening.
  3. Declared a struct stat variable named fileStat to hold the metadata.
  4. Used fstat(fileno(fp), &fileStat) to fetch metadata from the file descriptor obtained by fileno(fp).
  5. Displayed the file size using fileStat.st_size and closed the file using fclose(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

</>
Copy
#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:

  1. Declared a struct stat variable named linkStat to store the metadata of the symbolic link.
  2. Called lstat("link_to_example.txt", &linkStat) to retrieve metadata about the symbolic link.
  3. Checked if lstat returned a value less than 0 to handle potential errors.
  4. Displayed the file size from linkStat.st_size and the link mode (permissions) from linkStat.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.