Linux目录操作

mkdir()

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

opendir()、fdopendir()

//打开一个文件夹流,返回一个绑定了这个流的指针,成功返回一个指针,失败返回NULL设errno
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);

readdir()

//返回一个代表了目录流里下一个目录入口的结构体指针
a) #include <dirent.h>
struct dirent *readdir(DIR *dirp);
struct dirent {
    ino_t           d_ino;      /* inode number */
    off_t               d_off;      /* not an offset; see NOTES */
    unsigned    short   d_reclen;       /* length of this record */
    unsigned char       d_type;         /* type of file; not supported by all filesystem types */
    char                d_name[256];    /* filename */
};

telldir()

//返回当前的目录流的位置,失败返回-1设errno
#include <dirent.h>
long telldir(DIR *dirp);

seekdir()

设置下次读取目录的位置
#include <dirent.h>
void seekdir(DIR *dirp, long loc);

rewinddir()

//将目录流的位置指针重设到目录的开头
#include <sys/types.h>
#include <dirent.h>
void rewinddir(DIR *dirp);

dirfd()

//返回绑定了目录流的文件描述符
#include <sys/types.h>
#include <dirent.h>
int dirfd(DIR *dirp);

closedir()

关闭dirp绑定的目录流,成功返回0.失败返回-1设errno
a) #include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);

rmdir()

//删除指定路径的目录,这个目录必须是空的,成功返回0.失败返回-1
#include <unistd.h>
int rmdir(const char *pathname);
/*--------------------------------------------------------------------------------------------
hw.c编程实现打印指定目录中的内容, 要求子目录中的内容也要打印出来
--------------------------------------------------------------------------------------------*/
#include<unistd.h>
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print(char *path){
    DIR *dir=opendir(path);
    if(NULL==dir)
        perror("opendir"),exit(-1);
    printf("打开%s目录成功\n",path);
    struct dirent* ent=NULL;
    while(ent=readdir(dir)){
        printf("type:%d,name:%s\n",ent->d_type,ent->d_name);
        if(4==ent->d_type&&*ent->d_name!='.'){  
            char buf[200];
//          char buf[]="./";    //ATTENTION: 当前工作目录里找"./Code"是不行的,得在"../../160510/Code"里面找
//          strcat(buf,path);   //
            strcpy(buf,path);
            strcat(buf,"/");
            strcat(buf,ent->d_name);
            print(buf);
        }
    }
    int res=closedir(dir);
    if(-1==res)
        perror("closedir"),exit(-1);
}
int main(){
    print("../../160510");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值