目录IO:
1. mkdir
int mkdir(const char *pathname, mode_t mode);
功能:创建目录文件
参数:
pathname:文件路径
mode:文件的权限
rwx rwx rwx
111 111 111
0 7 7 7
r:目录中是否能够查看文件
w:目录中是否能够新建文件
x:目录是否能够进入
返回值:
成功返回0
失败返回-1
2. rmdir:
int rmdir(const char *pathname);
功能:删除空目录文件
返回值:
成功返回0
失败返回-1
3. opendir
DIR *opendir(const char *name);
功能:打开目录获得目录流指针
参数:
name:目录文件路径
返回值:
成功返回目录流指针
失败返回NULL
4. closedir
int closedir(DIR *dirp);
功能:关闭目录流指针
5. readdir
struct dirent *readdir(DIR *dirp);
功能:从目录流中读取下一个目录项的结构体信息
参数:
dirp:目录流指针
返回值:
成功返回包含目录项信息的空间首地址
失败返回NULL
读到文件末尾返回NULL
struct dirent {
ino_t d_ino; /* Inode number */
off_t d_off; /* Not an offset; see below */
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]; /* Null-terminated filename */
};
6. chdir
int chdir(const char *path);
功能:切换当前代码的工作路径
7. getcwd
char *getcwd(char *buf, size_t size);
功能:获得当前目录的绝对路径
8. access
int access(const char *pathname, int mode);
功能:检测调用函数的程序对文件是否拥有指定权限
参数:
pathname:文件路径
mode:
R_OK 检测是否拥有读权限
W_OK 检测是否拥有写权限
X_OK 检测是否拥有执行权限
F_OK 检测文件是否存在
返回值:
有该权限返回0
出错返回-1
作业:
1. 执行两次代码,打印出两次执行过程中新增的文件及删除的文件
#include "head.h"
int ListFile(const char *pdirname, const char *pfilename)
{
FILE *fp = NULL;
DIR *dp = NULL;
struct dirent *pp = NULL;
char tmpbuff[4096] = {0};
fp = fopen(pfilename, "a");
if(fp == NULL)
{
perror("fail to fopen");
return -1;
}
dp = opendir(pdirname);
if(dp == NULL)
{
perror("fail to opendir");
return -1;
}
while(1)
{
pp = readdir(dp);
if(pp == NULL)
{
break;
}
if(*pp->d_name == '.')
{
continue;
}
sprintf(tmpbuff, "%s/%s", pdirname, pp->d_name);
fprintf(fp, "%s\n", tmpbuff);
if(pp->d_type == DT_DIR)
{
ListFile(tmpbuff, pfilename);
}
}
fclose(fp);
closedir(dp);
return 0;
}
int ListDir(const char *pdirname, const char *pfilename)
{
FILE *fsrc = NULL;
FILE *fdst = NULL;
DIR *dp = NULL;
struct dirent *pp = NULL;
char tmpbuff[4096] = {0};
char tmpsrc[4096] = {0};
char tmpdst[4096] = {0};
char *psrc = NULL;
char *pdst = NULL;
fsrc = fopen(pfilename, "r");
if(fsrc == NULL)
{
perror("fail to fopen");
return -1;
}
fdst = fopen("b.txt", "w+");
if(fdst == NULL)
{
perror("fail to fopen");
return -1;
}
dp = opendir(pdirname);
if(dp == NULL)
{
perror("fail to opendir");
return -1;
}
ListFile(pdirname, "b.txt");
fseek(fsrc, 0, SEEK_SET);
fseek(fdst, 0, SEEK_SET);
while(1)
{
pdst = fgets(tmpdst, sizeof(tmpdst), fdst);
if(pdst == NULL)
{
break;
}
int found1 = 0;
fseek(fsrc, 0, SEEK_SET);
while(1)
{
psrc = fgets(tmpsrc, sizeof(tmpsrc), fsrc);
if(psrc == NULL)
{
break;
}
if(strcmp(tmpsrc, tmpdst) == 0)
{
found1 = 1;
break;
}
}
if(!found1)
{
printf("add:%s\n", tmpdst);
}
}
fseek(fsrc, 0, SEEK_SET);
fseek(fdst, 0, SEEK_SET);
while(1)
{
psrc = fgets(tmpsrc, sizeof(tmpsrc), fsrc);
if(psrc == NULL)
{
break;
}
int found2 = 0;
fseek(fdst, 0, SEEK_SET);
while(1)
{
pdst = fgets(tmpdst, sizeof(tmpdst), fdst);
if(pdst == NULL)
{
break;
}
if(strcmp(tmpdst, tmpsrc) == 0)
{
found2 = 1;
break;
}
}
if(!found2)
{
printf("mul:%s\n", tmpsrc);
}
}
fclose(fsrc);
fclose(fdst);
closedir(dp);
return 0;
}
int main(int argc, const char *argv[])
{
int flat = 0;
if(argc != 3)
{
fprintf(stderr, "Usage:./a.out dirname filename\n");
return -1;
}
flat = access(argv[2], F_OK);
if(flat == 0)
{
ListDir(argv[1], argv[2]);
}
else
{
ListFile(argv[1], argv[2]);
}
return 0;
}