In this blog we are going to learn How to get file size using stat() system call in C/C++.  Stat system call under Linux displays file or file system status. We require to include sys/stat.h header to use stat() system call. Let us see simple example of using stat system call  in c/c++ program.

Example of How to get file size using stat() system call in C/C++

#include<stdio.h>
#include<sys/stat.h>

int GetFileSize(const char *filename) {
	struct stat st;

	if (stat(filename, &st) != 0) {
		return -1;
	}
	return st.st_size;
}

int main() {
	char filename[100];
	int fsize = 0;
	printf("Enter Path of the file to get size\n");
	scanf("%s", filename);
	fsize = GetFileSize(filename);
	printf("The size of file %s is %d\n", filename, fsize);
	return 0;
}

Some More about stat() system call

  The stat system calls return a stat structure, which contains the following fields:

struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for filesystem I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

Sample code for stat system call to get file mode and last access time

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

time_t getLastAccessTime(const char *filename) {
	struct stat st;

	if (stat(filename, &st) != 0) {
		return -1;
	}

	return st.st_atime;
}

mode_t getMode(const char *filename) {
	struct stat st;

	if (stat(filename, &st) != 0) {
		return -1;
	}
	return st.st_mode;
}

char* mode_to_string(mode_t m, size_t s) {
	char *modestr = (char*) malloc(s);
	modestr[0] = (m & S_IRUSR) ? 'r' : '-';
	modestr[1] = (m & S_IWUSR) ? 'w' : '-';
	modestr[2] = (m & S_IXUSR) ? 'x' : '-';
	modestr[3] = (m & S_IRGRP) ? 'r' : '-';
	modestr[4] = (m & S_IWGRP) ? 'w' : '-';
	modestr[5] = (m & S_IXGRP) ? 'x' : '-';
	modestr[6] = (m & S_IROTH) ? 'r' : '-';
	modestr[7] = (m & S_IWOTH) ? 'w' : '-';
	modestr[8] = (m & S_IXOTH) ? 'x' : '-';
	modestr[9] = '\0';
	return modestr;
}

int main() {
	char filename[100];
	printf("Enter Path of the file \n");
	scanf("%s", filename);

	mode_t m = getMode(filename);
	time_t t = getLastAccessTime(filename);
	printf("Last file access:  %s", ctime(&t));
	printf("File mode is %s\n", mode_to_string(m, 10));

	return 0;
}

 

Ref:

https://en.wikipedia.org/wiki/Stat_(system_call)



Related Contents to follow