1.目录文件的操作:
1. mkdir
int mkdir(const char *pathname, mode_t mode);
功能:
创建目录
参数:
pathname:目录文件的路径
mode:目录文件的权限
返回值:
成功返回0
失败返回-1
注意:
1.r 决定是否目录下可以查看其余文件信息
2.w 决定目录下的文件是否能够新建
3.x 决定目录文件是否能够进入
2.rmdir
int rmdir(const char *pathname);
功能:
删除空目录文件
参数:
pathname:
目录文件的路径
返回值:
成功返回0
失败返回-1
3.getcwd
char *getcwd(char *buf, size_t size);
功能:
获得当前工作目录的绝对路径
参数:
buf:存放路径空间首地址
size:最大存放字符的长度
返回值:
成功返回存放路径字符串空间首地址
失败返回NULL
4.chdir
int chdir(const char *path);
功能:
切换程序当前的工作路径
参数:
path:目的路径
返回值:
成功返回0
失败返回-1
#include "../head.h" int main(void) { char tmpbuff[4096] = {0}; getcwd(tmpbuff, sizeof(tmpbuff)); printf("当前路径:%s\n", tmpbuff); chdir("/home/linux/Desktop"); getcwd(tmpbuff, sizeof(tmpbuff)); printf("当前路径:%s\n", tmpbuff); mkdir("dirname", 0777); rmdir("dirname"); return 0; }
5.chmod
chmod 0777 filename
chmod 0664 filename
chmod +/-r/w/x filename
int chmod(const char *pathname, mode_t mode);
功能:
修改文件权限
参数:
pathname:文件路径
mode:权限值 0777 0664
返回值:
成功返回0
失败返回-1
3.目录文件的读取:
1.操作方法: 打开目录
读取目录项
关闭目录
2.函数接口:
1.opendir
DIR *opendir(const char *name);
功能:
打开目录
参数:
name:目录文件路径
返回值:
成功返回目录流指针
失败返回NULL
2.closedir
int closedir(DIR *dirp);
功能:
关闭目录流指针
参数:
dirp:目录流指针
返回值:
成功返回0
失败返回-1
int main(void) { DIR *dp = NULL; struct dirent *pp = NULL; mkdir("dirname", 0777); fclose(fopen("dirname/a.txt", "w")); fclose(fopen("dirname/b.txt", "w")); fclose(fopen("dirname/c.txt", "w")); fclose(fopen("dirname/d.txt", "w")); fclose(fopen("dirname/e.txt", "w")); dp = opendir("dirname"); if (NULL == dp) { perror("fail to opendir"); return -1; } while (1) { pp = readdir(dp); if (NULL == pp) { break; } printf("=======================\n"); printf("inode: %ld\n", pp->d_ino); printf("offset: %ld\n", pp->d_off); printf("reclen: %d\n", pp->d_reclen); printf("name: %s\n", pp->d_name); printf("=======================\n"); } closedir(dp); return 0; }
3.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 */
};
路径: ../file.txt
文件名:file.txt
#include "../head.h" int ListDir(char *pdirname) { DIR *dp = NULL; struct dirent *pp = NULL; dp = opendir(pdirname); if(NULL == dp) { return -1; } while (1) { pp = readdir(dp); if (NULL == pp) { break; } if ('.' == *pp->d_name) { continue; } printf("pp->d_name = %s/%s\n", pdirname, pp->d_name); } closedir(dp); return 0; } int main(void) { char dirname[256] = {0}; printf("请输入目录路径:\n"); scanf("%s", dirname); ListDir(dirname); return 0; }
4.链接文件:
1.软链接(符号链接)
ln -s 要链接向的文件名 软链接文件名
ln -s b.txt a.txt
a.txt -> b.txt
通过文件名进行链接
普通文件:
文件名 -> inode -> 数据块
软链接:
软连接文件名 -> inode -> 数据块 -> 链接向的文件名 -> inode -> 数据块
#include "../head.h" int ListDir(char *pdirname) { char filepath[1024] = {0}; DIR *dp = NULL; struct dirent *pp = NULL; dp = opendir(pdirname); if (NULL == dp) { return -1; } while (1) { pp = readdir(dp); if (NULL == pp) { break; } if ('.' == *pp->d_name) { continue; } sprintf(filepath, "%s/%s", pdirname, pp->d_name); printf("%s\n", filepath); if (DT_DIR == pp->d_type) { ListDir(filepath); } } closedir(dp); return 0; } int main(void) { char dirname[256] = {0}; struct timeval start; struct timeval end; long ts; printf("请输入目录路径:\n"); scanf("%s", dirname); gettimeofday(&start, NULL); ListDir(dirname); gettimeofday(&end, NULL); ts = (end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec); printf("耗时 %ld.%ld s\n", ts / 1000000, ts % 1000000); return 0; }
int symlink(const char *target, const char *linkpath);
功能:
创建一个linkpath的软连接文件,里面存放target字符串
参数:
target:链接向的文件名
linkpath:软链接文件名
返回值:
成功返回0
失败返回-1
#include "../head.h" int main(void) { FILE *fp = NULL; char tmpbuff[4096] = {0}; symlink("file.txt", "a.txt"); fp = fopen("a.txt", "r"); if (NULL == fp) { perror("fail to fopen"); return -1; } fgets(tmpbuff, sizeof(tmpbuff), fp); printf("%s\n", tmpbuff); fclose(fp); memset(tmpbuff, 0, sizeof(tmpbuff)); readlink("a.txt", tmpbuff, sizeof(tmpbuff)); printf("连接文件本身的数据:%s\n", tmpbuff); return 0; }
ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
功能:
读取软链接文件本身内容
参数:
path:软链接文件名
buf:存放软链接文件内容的缓冲区
bufsize:缓冲区的大小
返回值:
成功返回读取的字节数
失败返回-1
2.硬链接
ln 要链接的文件名 硬链接文件名
通过在磁盘中存放的inode节点进行链接
删除文件链接关系断开
link
int link(const char *oldpath, const char *newpath);
功能:
创建一个newpath的硬链接文件
参数:
oldpath:要链接的文件名
newpath:硬链接文件名
返回值:
成功返回0
失败返回-1
unlink
int unlink(const char *pathname);
功能:
删除链接文件名,并让硬链接个数-1 ,如果一个磁盘空间硬链接个数为0,需要回收磁盘空间
参数:
pathname:链接文件名
返回值:
成功返回0
失败返回-1
#include "../head.h" int main(void) { link("file.txt", "a.txt"); unlink("file.txt"); return 0; }
5.stat和lstat
int lstat(const char *pathname, struct stat *statbuf);
功能:
获得pathname对应文件的详细信息
参数:
pathname:文件路径
statbuf:存放文件详细信息空间的首地址
返回值:
成功返回0
失败返回-1
#include "../head.h" int DisplayFile(char *pfilepath) { struct stat buf; int ret = 0; ret = lstat(pfilepath, &buf); if (-1 == ret) { return -1; } switch (buf.st_mode & S_IFMT) { case S_IFREG:putchar('-');break; case S_IFSOCK:putchar('s');break; case S_IFLNK:putchar('l');break; case S_IFBLK:putchar('b');break; case S_IFDIR:putchar('d');break; case S_IFCHR:putchar('c');break; case S_IFIFO:putchar('p');break; } putchar(' '); printf("%ld ", buf.st_nlink); printf("%d ", buf.st_uid); printf("%d ", buf.st_gid); printf("%ld ", buf.st_size); printf("%ld ", buf.st_mtime); printf("%s\n", pfilepath); return 0; } int main(void) { char filepath[256] = {0}; printf("请输入文件路径:\n"); scanf("%s", filepath); DisplayFile(filepath); return 0; }
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
nlink_t st_nlink; /* Number of hard links */
uid_t st_uid; /* User ID of owner */
gid_t st_gid; /* Group ID of owner */
dev_t st_rdev; /* Device ID (if special file) */
off_t st_size; /* Total size, in bytes */
blksize_t st_blksize; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
/* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};