How to get File Size timestamp access status in C

In this article, we are going to learn how to get File Size timestamp access status in C. In the C library, we have two functions that help us find the file information like the size in bytes. These functions are:

How to get File Size timestamp access status in C Langauge


In this c program, we are making use of the stat() function to find the size of the file in the C code. This function is available as a system call. The arguments in this function are, the path to the file and a structure that gets filled with the file-related data like size, timestamp, last modified time, etc. We need to include #include the header file in our program to use the stat() function.Let us understand this with the help of the below program example.

1. C Program to get File Size timestamp access status


To run this c program, we need one text file with the name Readme.txt in the same folder where we have our code. The content of the file is:

Hello My name is 
John 
danny
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

const char *filename = "Readme.txt";

int main(int argc, char *argv[]) 
{
    struct stat sb;

    if (stat(filename, &sb) == -1) 
	{
        perror("stat");
        return 0;
    }

    printf("Total Size of the file in Bytes: %lu\n", sb.st_size);
    printf("Time of Last status change:       %s", ctime(&sb.st_ctime));
    printf("Time of Last file access:         %s", ctime(&sb.st_atime));
    printf("Time of Last file modification:   %s", ctime(&sb.st_mtime));

    return 0;
}

Output

Total Size of the file in Bytes: 35
Time of Last status change:       Wed Mar 16 12:44:08 2022
Time of Last file access:         Wed Mar 16 12:44:08 2022
Time of Last file modification:   Wed Mar 16 12:44:08 2022

2. How to get File Size timestamp access status in C using fstat function


In this example, we are making use of the fstat() function to find the size of the file in C code.The fstat() function is a little different from stat() function when it comes to the procedure to use it.To use fstat() function , we need the file descriptor, instead of the file path. We need to use open() function to get the file descriptor, then we pass this file descriptor as first argument to fstat() function.

We need to include #include header file in our program to use the fstat() function.This function is available as a system call. The second argument in the fstat() function is a structure that gets filled with the file related data like size , timestamp, last modified time etc.Let us understand this with the help of below code example

Most Popular Post


C Program to get File Size timestamp access status

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

const char *filename = "Readme.txt";

int main(int argc, char *argv[]) 
{
    struct stat sb;

    int fd = open(filename, O_RDONLY);
    if (fd == -1) 
	{
        perror("open");
        return 0;
    }

    if (fstat(fd, &sb) == -1) 
	{
        perror("stat");
        return 0;
    }

	printf("Total Size of the file in Bytes: %lu\n", sb.st_size);
    printf("Time of Last status change:       %s", ctime(&sb.st_ctime));
    printf("Time of Last file access:         %s", ctime(&sb.st_atime));
    printf("Time of Last file modification:   %s", ctime(&sb.st_mtime));
	

    close(fd);

    return 0;
}

Output

Total Size of the file in Bytes: 35
Time of Last status change:       Wed Mar 16 12:51:07 2022
Time of Last file access:         Wed Mar 16 12:51:07 2022
Time of Last file modification:   Wed Mar 16 12:51:07 2022

Summary

In this post we have learned how to get File Size timestamp access status in C with examples by using stat() function,fstat() function.