C++ || Snippet – How To Display The Size Of A File Using Stat

Print Friendly, PDF & Email

The following is sample code which demonstrates the use of the “stat” function calls on Unix based systems.

The stat function returns information about a file. No permissions are required on the file itself, but-in the case of stat() and lstat() – execute (search) permission is required on all of the directories in path that lead to the file.

The “stat” system call returns a stat structure, which contains the following fields:

The following example demonstrates the use of the “st_size” struct parameter to display the size of a file.

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

The following is sample output:

./Stat Stat.cpp

The file "Stat.cpp" is 1170 bytes

Leave a Reply