c - 我的 ls -R 不起作用

标签 c struct directory ls stat

我的 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/

相关文章:

c# - 在C#中创建表示文件夹结构(包括子文件夹)的XML文件

c - IFileOperation::DeleteItem 在系统目录 .Local 中不起作用

c - 如何获取c程序的执行时间?

将结构转换为可变长度缓冲区

c - sizeof 运算符返回奇怪的结果

c# - 如何在 C# 中重命名文件夹/目录?

c - 对我的 Atmega644 MCU 进行编程时。为什么 PORTD |= 0b00100000 有效,但 PORTD |= (PD5 <<1) 无效?

c - 不使用第三个变量交换字符

c - 是否可以使用 isdigit() 函数测试 float ?

c - 制作结构数组和整理单词时出现问题