[英]How is this while-loop iterating?
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#define ERR(source) (perror(source),\
fprintf(stderr, "%s:%d\n", __FILE__, __LINE__),\
exit(EXIT_FAILURE))
void scan_dir() {
DIR* dirp;
struct dirent *dp;
struct stat filestat;
if (NULL == (dirp = opendir("."))) ERR("opendir");
do {
errno = 0;
if ((dp = readdir(dirp)) != NULL) {
if (lstat(dp->d_name, &filestat)) ERR("lstat");
printf("%s\n", dp->d_name);
}
}
while (dp != NULL);
}
我理解这里的大部分代码,但我无法弄清楚dp
每次如何更改/迭代。 我想可能是dp = readdir(dirp)
,即每次都将dp
的值分配给另一个目录入口,但我不确定,如果是,它如何自动将其分配给下一个?
在 C 中, =
是一个运算符,就像其他所有运算符一样。 a = b
将变量a
设置为值b
,然后返回值b
。 因此, y = (x = 1) + 2
将 x 设置为 1,然后将 2 添加到 1,然后将 y 设置为 3。 readdir
将DIR*
作为参数,并且DIR*
具有确定哪个文件的内部状态阅读。 每次调用readdir
时,它都会返回下一个文件。 请参阅此处的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.