小编sko*_*oya的帖子

C列出其他目录中目录的名称

我正在写一个列出目录名称的程序。这不是一个非常复杂的代码,但是启动程序后,我得到了奇怪的错误

#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>


int main() {
    struct dirent * dir;
    struct stat buf;
    DIR *d;
    if(d = opendir("/..."))
    {
        while(dir = readdir(d))
        {
            if(S_ISDIR(buf.st_mode))
                puts(dir->d_name);
            closedir(d);
        }
    }
    else
        perror("read");
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

*** Error in `./names.exe': double free or corruption (top): 0x00000000011d3010 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81499)[0x7f702b1bc499]
/lib64/libc.so.6(closedir+0xd)[0x7f702b1fbaed]
./names.exe[0x40068d]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f702b15d445]
./names.exe[0x400579]
======= Memory map: ========

Run Code Online (Sandbox Code Playgroud)

我做错了什么?

c system-calls

0
推荐指数
1
解决办法
29
查看次数

标签 统计

c ×1

system-calls ×1