stat() giving wrong directory size in c

后端 未结 3 774
庸人自扰
庸人自扰 2021-01-21 00:16

I need to find the size of a file or a directory whatever given in the commandline using stat(). It works fine for the files (both relative and absolute paths) but when I give a

3条回答
  •  心在旅途
    2021-01-21 01:01

    stat() on a directory doesn't return the sum of the file sizes in it. The size field represents how much space it taken by the directory entry instead, and it varies depending on a few factors. If you want to know how much space is taken by all files below a specific directory, then you have to recurse down the tree, adding up the space taken by all files. This is how tools like du work.

提交回复
热议问题