我的 ls -R 在 C 中实现时出现问题。当我使用命令 ./myls -R 运行程序时,没有任何显示,全是空白:(需要帮助。
int print_Recursive(char *dirname)
{
char fbuf [256];
DIR *dir;
struct dirent *dirp;
struct stat sbuf;
printf("\n");
dir = opendir(dirname);
while ((dirp = readdir(dir)))
{
if(strcmp(dirp->d_name, ".") != 0 &&
strcmp(dirp->d_name, "..") != 0)
{
sprintf(fbuf, "%s/%s", dirname, dirp->d_name);
}
}
closedir(dir);
return 0;
}
int print_file(char *file, char *dir, struct stat buf, int showinode, int showlong, int showRec)
{
if (showinode)
printf("%lld ", buf.st_ino);
if (showlong)
print_long(file, dir, buf);
if (showRec)
print_Recursive(dir);
else
printf("%s\n", file);
return 0;
}
无法弄清楚我做错了什么:(
最佳答案
您使用sprintf
将值存储在fbuf
中,但您从不打印fbuf
,因此不会出现任何内容。尝试在 while 循环后添加此行:
printf("%s", fbuf);
关于c - 我的 ls -R 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510752/