unix - 文件和目录

readdir函数

       #include <dirent.h>

       struct dirent *readdir(DIR *dirp);

The readdir() function returns a pointer to a dirent structure
representing the next directory entry in the directory stream
pointed to by dirp. It returns NULL on reaching the end of the
directory stream or if an error occurred.

ls的实现代码如下

#include <stdio.h>          
#include <stdlib.h>        
#include <stddef.h>        
#include <string.h>         
#include <unistd.h>      
#include <dirent.h>



int 
main (int argc,  char *argv[])
{
    int c;
    
    while((c=getc(stdin))!=EOF)
    {
        if(putc(c,stdout)==EOF)
        {
            printf("output error\n");
            exit(1);
        }
    }

    if(ferror(stdin))
    {
        printf("input error\n");
        exit(1);        
    }

    exit(0);
}
上一篇:Oracle安装报错libnsl.so.1: cannot open shared object file: No such file or directory


下一篇:[C#][转载]如何删除目录中的所有文件和文件夹