文件和文件夹管理 - 文件夹

2019-09-26  本文已影响0人  无无吴

获取当前工作目录

#include <unistd.h>
char * getcwd(char *buf, size_t size);

成功会把路径名写到buf中,失败返回NULL,并设置errno。

char cwd[BUF_LEN];
if(!getcwd(cwd, BUF_LEN)){
      perror("getcwd");
      exit(EXIT_FAILURE);
}
printf("cwd = %s\n", cwd);
//linux special
char *cwd;
cwd = getcwd(NULL, 0);
if(!cwd){
        perror("getcwd");
        exit(EXIT_FAILURE);
}
printf("cwd = %s\n", cwd);
free(cwd);
//linux special
#define _GNU_SOURCE
#include <unistd.h>
char *get_current_dir_name(void);
char *cwd;
cwd = get_current_dir_name();
if(!cwd){
        perror("get_current_dir_name");
        exit(EXIT_FAILURE);
 }
printf("cwd = %s\n", cwd);
free(cwd);

改变当前工作目录(cd)

#include <unistd.h>
int chdir(const char *path);
int fchdir(int fd);

前者改变工作目录到path指定的绝对或相对路径,类似的后者改变工作路径到fd代表的路径名。
成功返回0,失败返回-1,并设置errno。

最普通的getcwd的用法是保存当前的工作目录以便进程能在之后返回。

char *swd;
int ret;
/* save the current working directory */ 
swd = getcwd(NULL,0);
if(!swd){
      perror("getcwd");
      exit(EXIT_FAILURE);
}
/* change to a different directory*/
ret = chdir(some_other_dir);
if(ret){
    perror("chdir");
    exit(EXIT_FAILURE);
}
/* do some other work in the new directory*/
ret = chdir(swd);
if(ret){
        perror("chdir");
        exit(EXIT_FAILURE);
}
free(cwd);
// better one
int swd_fd;
swd_fd = open(".", O_RDONLY);
if(swd_fd == -1){
        perror("open");
        exit(EXIT_FAILURE);
}
/*change to a different directory*/
ret = chdir(some_other_dir);
if(ret){
    perror("chdir");
    exit(EXIT_FAILURE);
}
/* do some other work in the new diretory*/
/* return to the saved directory*/
ret = fchdir(swd_fd);
if(ret){
    perror("fchdir);
    exit(EXIT_FAILURE);
}
/*close the directory' fd*/
ret = close(swd_fd);
if(ret){
        perror("close");
        exit(EXIT_FAILURE);
}

创建目录

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

成功返回0,失败返回-1,并设置errno。

移除目录

#include <unistd.h>
int rmdir(const char *path);

成功返回0,失败返回-1,并设置errno。

int ret;
/* remove the directory /home/barbary/maps */
ret = rmdir("/home/barbary/maps");
if(ret)
        perror("rmdir");

读取文件夹中的内容

要去读取文件夹中的内容,你必须先创建一个directory stream。

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);

成功返回一个directory stream指代name。

#define _BSD_SOURCE /* or _SVID_SOURCE*/
#include <sys/types.h>
#include <dirent.h>
int dirfd(DIR *dir);

成功返回一个指代dir的fd。失败返回-1,并设置errno。

从directory stream中读取内容

#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dir);
struct dirent {
          ino_t d_ino; /* inode number */
          off_t d_off; /* offset to the next dirent */
          unsigned short d_reclen; /* length of this record */
          unsigned char d_type; /* type of file */
          char d_name[256]; /* filename */
};

失败返回NULL。
每次用这个函数之前必须设置errno为0,并且每次调用必须check返回值和errno。
readdir()设置的唯一errno值是EBADF,表示dir无效。

关闭directory stream

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dir);

成功返回0,失败返回-1。

find_file_in_dir(),它使用readdir()搜索给定目录中的给定文件名。如果文件存在于目录中,则函数返回0。否则,它将返回一个非零值:

/*
 * find_file_in_dir - searches the directory 'path' for a
 * file named 'file'.
 *
 * Returns 0 if 'file' exists in 'path' and a nonzero
 * value otherwise.
 */
int find_file_in_dir(const char *path, const char *file)
{
        struct dirent *entry;
        int ret = 1;
        DIR *dir;

        dir = opendir(path);
        while((entry = readdir(dir)) != NULL){
                  if(strcmp(entry->d_name, file) == 0){
                            ret = 0;
                            break;
                  }
        }
        if(errno && !entry)
              perror("readdir");
        closedir(dir);
        return ret;
}
上一篇下一篇

猜你喜欢

热点阅读