struct stat st; stat(path,&st); if(S_ISDIR(st.st_mode)) { return 1; } else{ return 0; }
时间: 2023-02-25 13:06:29 浏览: 41
&st)是什么意思?
"struct stat st; stat(path, &st)" 意思是:在程序中定义了一个名为 "st" 的结构体变量,该变量的类型是 "struct stat"。随后,调用了 "stat" 函数,并将两个参数传递给该函数:一个是文件的路径名 "path",另一个是 "st" 的地址 "&st"。
这个函数的作用是用于获取文件的信息,并将这些信息存储在 "st" 结构体变量中,以便在程序的其他地方使用这些信息。
相关问题
bool isDirectory(char* path) { struct stat ss; stat(path, &ss); return S_ISDIR(ss.st_mode) ? true : false; }的意思
这段代码的作用是判断给定的路径是否为一个目录(文件夹),返回值为布尔类型。它使用了 C 语言中的 stat 函数来获取给定路径的文件信息,包括文件类型、访问权限、大小等等。然后通过宏定义 S_ISDIR(ss.st_mode) 来判断这个文件是否为目录。如果是目录,则返回 true,否则返回 false。
include "head.h" int main(int argc, char *argv[]) { if (argc != 2) { printf("%s<dirname>\n", argv[0]); return -1; } struct dirent *dt; struct stat st; struct tm *tp; struct passwd *pd; struct group *gd; char pathname[300]; DIR *dir = opendir(argv[1]); while((dt= readdir(dir))!=NULL) { if (!strcmp(dt->d_name, ".") || !strcmp(dt->d_name, "..")) { continue; } memset(&stat, 0, sizeof(stat)); snprintf(pathname, strlen(argv[1]) + strlen(dt->d_name) + 2, "%s%s", argv[1], dt->d_name); if (lstat(pathname, &st) < 0) { perror("lstat"); return -1; } // 权限 if (S_ISREG(st.st_mode)) printf("-"); else if (S_ISDIR(st.st_mode)) printf("d"); else if (S_ISCHR(st.st_mode)) printf("c"); else if (S_ISBLK(st.st_mode)) printf("b"); else if (S_ISFIFO(st.st_mode)) printf("p"); else if (S_ISLNK(st.st_mode)) printf("l"); else if (S_ISSOCK(st.st_mode)) printf("s"); printf("111111111111111111111111\n"); int i; char *m = "xwr"; for (i = 8; i >= 0; i--) { if (st.st_mode & 1 << i) { printf("%c", m[i % 3]); } else { printf("-"); } } printf(" "); tp = localtime(&st.st_ctime); printf("%lu %s %s %ld", st.st_nlink, pd->pw_name, gd->gr_name, st.st_size); printf(" "); printf("%d月 ", tp->tm_mon + 1); printf("%d ", tp->tm_mday); printf("%d:%d ", tp->tm_hour, tp->tm_min); printf("%s ", pathname); } closedir(dir); return 0; }这段代码那里有问题
这段代码有一个问题是在使用 memset 函数清空结构体 st 时,传入了错误的参数。应该传入 &st 而不是 stat。正确的代码应该是:
```
memset(&st, 0, sizeof(st));
```
另外,pd 和 gd 指针变量在代码中没有进行初始化,可能会导致未定义的行为。应该使用 getpwuid 和 getgrgid 函数获取对应的用户和组信息。