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);
}
posted @ 2021-10-02 10:48  feiwatson  阅读(28)  评论(0编辑  收藏  举报