文件和目录管理全解析
1. 文件查找函数实现
在文件和目录管理中,经常需要在指定目录中查找特定文件。下面的代码实现了一个名为 find_file_in_dir() 的函数,它使用 readdir() 来搜索指定目录中的指定文件名。如果文件存在于目录中,函数返回 0;否则,返回非零值。
/*
* find_file_in_dir - searches the directory 'path' for a
* file named 'file'.
*
* Returns 0 if 'file' exists in 'path' and a nonzero
* value otherwise.
*/
int find_file_in_dir (const char *path, const char *file)
{
struct dirent *entry;
int ret = 1;
DIR *dir;
dir = opendir (path);
errno = 0;
while ((entry = readdir (dir)) != NULL) {
if (!strcmp(entry->d_name, file)) {
ret = 0;
break;
}
}
if (errno &&
超级会员免费看
订阅专栏 解锁全文
9694

被折叠的 条评论
为什么被折叠?



