查找目录的大小
我在思科面试时遇到这样的问题:编写一个函数来查找目录的大小?
以下是此类函数的伪代码,它遵循递归方法。请告诉我是否还有其他方法。
int directorySize(DirectoryHandle dh)
{
int size=0;
if (!dh)
{
DirectoryHandle dh1 = directoryOpen("Directory_path");
}
else
{
dh1 = dh;
}
while (dh1)
{
if (TRUE=IsDirectory(dh1))
{
size += directorysize(dh1);
}
else if (TRUE == IsFile(dh1))
{
FileHandle fh = dh1;
while (EOF != fh)
{
size++;
}
}
}
}
I got this question in a Cisco interview: write a function to find the size of a directory?
Following is the pseudocode for such a function, that follows a recursive approach. Please tell me if there can be any other approach also.
int directorySize(DirectoryHandle dh)
{
int size=0;
if (!dh)
{
DirectoryHandle dh1 = directoryOpen("Directory_path");
}
else
{
dh1 = dh;
}
while (dh1)
{
if (TRUE=IsDirectory(dh1))
{
size += directorysize(dh1);
}
else if (TRUE == IsFile(dh1))
{
FileHandle fh = dh1;
while (EOF != fh)
{
size++;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 nftw 的典型示例:
请注意,随着面试问题的进行,他们可能希望看到您考虑
以下代码确实以务实的方式解决了大多数这些问题:
.
这被发布为最快的方法获取目录大小和磁盘上的大小之前。典型输出:
Canonical example of using nftw:
Note that as interview questions go, they will probably want to see you thinking about
The following code does address most of these issues in a pragmatic fashion:
.
This was posted as Fastest ways to get a directory Size and Size on disk before. Typical output:
也许为大型文件集添加更多空间和更好的子目录导航。
Maybe adding more room for large file sets and a better subdirectory navigation.