发新话题
打印

一个关于目录操作的问题

一个关于目录操作的问题

部分代码:
复制内容到剪贴板
代码:
void do_ls(char * dirname){
DIR  *   dir_ptr;
dirent *  direntp;
if((dir_ptr=opendir(dirname)==NULL)
{
      printf("Open dir unsuccessful!\n");
      return;
}        
while((dirent=readdir(dir_ptr))!=NULL)
             printf("Name :%s \n",direntp->d_name);
closedir(dir_ptr);
}
问题:经检测程序运行到while里面的时候就出现提示段错误,请问会是什么问题呢 ?      

TOP

刚刚找出病根所在:5 {) g2 z3 `) D
发现读出的direntp->d_name  direntp->d_type 都为空!!!3 p* Y) }8 m( @- b$ k
为什么会这样呢?      

TOP

括号不匹配, 请再写一遍      
'
梅须逊雪三分白 雪却输梅一段香

TOP

复制内容到剪贴板
代码:
[color=blue]-(user@host:tty)-(tmp)-
[12857 0] $ [/color]cat readdir.c

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <assert.h>

int main(int argc, char *argv[])
{
    assert(argc == 2);

    DIR * pdir = opendir(argv[1]);
    assert(pdir);

    struct dirent * pent;

    while ( (pent = readdir(pdir)) ) {
        printf("%s\n", pent->d_name);
    }

    closedir(pdir);

    return 0;
}
[color=blue]-(user@host:tty)-(tmp)-
[12857 0] $ [/color]gcc readdir.c
[color=blue]-(user@host:tty)-(tmp)-
[12857 0] $ [/color]./a.out /data/
.
..
lost+found
software
[color=blue]-(user@host:tty)-(tmp)-
[12857 0] $ [/color]ls /data/
lost+found  software
[color=blue]-(user@host:tty)-(tmp)-
[12857 0] $ [/color]
      
'
梅须逊雪三分白 雪却输梅一段香

TOP

刚刚弄出来了  可以显示出来了  6 ?! J. n7 a  z! Y6 _; N9 ~" Z; H
不过请问你高亮 显示的东西是什么意思? 我不明白 谢谢      

TOP

什么高亮显示?      
'
梅须逊雪三分白 雪却输梅一段香

TOP

发新话题