<分区>
我是 C 初学者,我对如何做到这一点感到有点困惑。我正在尝试使用 readdir
和 strcmp
函数,但它会给我带来很多错误。
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dirp;
struct dirent *direntp;
dirp = opendir(argv[1]);
if (dirp == NULL)
{
printf("File could not be open\n");
return -1;
}
int i = 0;
while((direntp[i]=readdir(dirp)) != NULL)
{
if(strcmp(direntp[i], argv[2]) == 0)
{
printf("The file %d is in directory %s my friend!", argv[2], dirp);
}
i++;
}
closedir(dirp);
return 0;
}