1.获取文件的属性
函数功能:获取一个文件的属性
函数头文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
函数原型:
int stat(const char *pathname, struct stat *buf);
函数的参数:
pathname:(路径)你要查看哪个文件的属性
struct stat :提供这样的空间
返回值:
成功返回 0
失败返回 -1
lstat:唯一的区别
就是返回链接文件本身
fstat:
不要路径
他要这个文件的描述符(int)
需要open打开该文件传给这个函数
效果stat保持一致
2.文件属性的结构体
struct stat {
dev_t st_dev; /*如果是设备,返回设备表述符,否则为 0*/
ino_t st_ino; /* i 节点号 */
mode_t st_mode; /* 文件类型 */
nlink_t st_nlink; /* 链接数 */
uid_t st_uid; /* 属主 ID */
gid_t st_gid; /* 组 ID */
dev_t st_rdev; /* 设备类型*/
off_t st_size; /* 文件大小,字节表示 */
blksize_t st_blksize; /* 系统每次按块Io操作时块的大小(一般是512或1024)*/
blkcnt_t st_blocks; /*块的索引号 */
time_t st_atime; /* 最后访问时间,如read*/
time_t st_mtime; /* 最后修改时间*/
time_t st_ctime; /* 创建时间 */
};
3.文件属性如何判断
st_mode(); //检查文件的类型
S_ISLNK();//检查该文件是否为链接文件
S_ISREG();//检查该文件是否为普通文件
S_ISDIR();//目录文件
S_ISCHR();//字符文件
S_ISBLK();//块文件
S_ISFIFO();//管道文件
S_ISSOCK();//套接字文件
如果是 ,则返回1
如果不是 则返回0
4.检测文件的权限
函数功能:检测某个文件的权限
函数头文件:<unistd.h>
函数原型: int access(const char *pathname, int mode);
函数参数:
pathname : 路径
mode : 需要测试的权限,可以用 |
R_OK:测试读权限
W_OK:测试写权限
X_OK:测试执行权限
F_OK:测试文件是否存在
函数返回值:
成功 返回 0
失败 返回 -1
5.代码
#include "main.h"
int main(int argc,char * argv[])
{
int flag=0;
struct stat tmp;
flag = lstat("./TEST/1.txt",&tmp);
if(flag < 0)
{
perror("stat");
}
printf("节点号==%lu\n",tmp.st_ino);
printf("类型==%ud\n",tmp.st_mode);
printf("链接数==%lu\n",tmp.st_nlink);
printf("大小==%ld\n",tmp.st_size);
printf("时间==%s",ctime(&tmp.st_ctime));
if(S_ISREG(tmp.st_mode) == 1)
{
printf("普通文件\n");
}else if(S_ISDIR(tmp.st_mode)==1)
{
printf("目录文件\n");
}else if(S_ISLNK(tmp.st_mode)==1)
{
printf("链接文件\n");
}
flag = access("./TEST/1.txt",R_OK|W_OK);
if(flag == 0)
{
printf("该文件能读能写\n");
}else
{
printf("该文件不能读或者不能写\n");
}
return 0;
}
结果:
6.目录基本函数
6.1 mkdir函数
函数功能: 创建一个目录
函数头文件:
#include <sys/stat.h>
#include <sys/types.h>
函数的原型:
int mkdir(const char *pathname, mode_t mode);
函数的参数:
pathname:你要哪里创建文件夹
mode :权限0755 给执行权限
函数返回值:
成功 0
失败 -1
6.2 rmdir函数
函数功能:删除一个空文件夹
函数头文件:
#include <unistd.h>
函数的原型:
int rmdir(const char *pathname);
函数的参数:
pathname:你需要删除的文件夹
函数返回值:
成功 0
失败 -1
代码:
void test_dir_mk_rm_pwd(void)
{
char * str;
char buf[256];
mkdir("./USER/TEST1",0755);
sleep(2);
rmdir("./USER/TEST1");
str = getcwd(buf,256);
printf("%s\n",str);
printf("%s\n",buf);
printf("地址=%p\n",str);
printf("地址=%p\n",buf);
}
6.3 getcwd函数
函数功能:获取文件的绝对路径
函数头文件:unistd.h
函数的原型:char *getcwd(char *buf,size_t size);
函数的参数:
buf :就是你要存储的地方(有空间)
size:你buf的字节数
(路径长度超过size就会溢出)
函数返回值:
返回的就是buf的首地址
6.4 chdir函数
函数功能:跳转目录
函数头文件:
#include <unistd.h>
函数原型:
int chdir(const char *path);
函数参数:
path:你要跳转的路径(想对程序来说)
函数返回值:
成功 0
失败 -1
代码:
void test_cd(void)
{
char buf[256];
int flag =0;
flag = chdir("./USER");
if(flag<0)
{
perror("chdir");
}
printf("跳转成功\n");
getcwd(buf,256);
printf("%s\n",buf);
}
6.5 chmod函数
chmod +x +r +w 0777
函数功能:修改文件的权限
函数头文件:
#include <sys/types.h>
#include <sys/stat.h>
函数的原型:
int chmod(const char *path, mode_t mode);
函数的参数:
path:要修改的文件的路径
mode:你要给什么权限
函数返回值:
成功 0
失败 -1
代码:
//测试修改权限函数chmod
void test_chomd(void)
{
chmod("./USER",0777);
}
7.目录的读写操作
7.1 opendir函数
函数功能:打开一个目录返回目录句柄
函数头文件:
#include <sys/types.h>
#include <dirent.h>
函数的原型:
DIR *opendir(const char *name);
函数的参数:
name:你要打开哪个目录
函数返回值:
就是目录的句柄(不需要看内部)
7.2 closedir函数
函数功能:关闭一个目录句柄
函数头文件:
#include <sys/types.h>
#include <dirent.h>
函数的原型:
int closedir(DIR *dir);
函数的参数:
DIR *dir:你要关闭的文件句柄
函数返回值:
成功 0
失败 -1
7.3 readdir函数
函数功能:读取一个目录
函数头文件:
#include <sys/types.h>
#include <dirent.h>
函数的原型:
struct dirent *readdir(DIR *dirp);
函数的参数:
DIR : 目录句柄
函数返回值:
返回你读这个目录的信息结构体
普通文件: 1000 == 8
链接文件: 1010 == 10
目录文件: 0100 == 4
管道文件: 0001 == 1
字符文件: 0011 == 3
块文件: 0110 == 6
套接字文件: 1100 == 12
7.4 telldir函数
函数功能:读取目录读取位置的偏移量
函数头文件:
#include <sys/types.h>
#include <dirent.h>
函数的原型:
off_t telldir(DIR *dir);
10个 就行循环查看前四个文件
i++;
i>=4;
记录它的偏移量
seekdir(偏移量);
8.代码
#include "main.h"
#include "unistd.h"
#include <sys/types.h>
#include <dirent.h>
#include "dirtest.h"
void test_cd(void);
void test_chomd(void);
int main(int argc, char * argv[])
{
Test_Seek_and_tell();
return 0;
}
void test_dir_mk_rm_pwd(void)
{
char * str;
char buf[256];
mkdir("./USER/TEST1",0755);
sleep(2);
rmdir("./USER/TEST1");
str = getcwd(buf,256);
printf("%s\n",str);
printf("%s\n",buf);
printf("地址=%p\n",str);
printf("地址=%p\n",buf);
}
//测试跳转目录函数
void test_cd(void)
{
char buf[256];
int flag =0;
flag = chdir("./USER");
if(flag<0)
{
perror("chdir");
}
printf("跳转成功\n");
getcwd(buf,256);
printf("%s\n",buf);
}
//测试修改权限函数chmod
void test_chomd(void)
{
chmod("./USER",0777);
}
#include "dirtest.h"
//测试目录的打开和关闭
void Test_dir_open_and_clsoe(void)
{
DIR * dir=NULL;
dir = opendir("./USER");
if(dir ==NULL)
{
perror("opendir");
return ;
}
printf("打开成功\n");
closedir(dir);
printf("关闭成功\n");
}
//测试第一个读出打目录属性
void test_read_dir(void)
{
long size;
struct dirent * dirstruct=NULL;
DIR * dir=NULL;
dir = opendir("./USER");
if(dir ==NULL)
{
perror("opendir");
return ;
}
printf("打开成功\n");
while(1)
{
dirstruct = readdir(dir);
if(dirstruct == NULL)
{
break;
}
printf("名字=%s\n",dirstruct->d_name);
printf("索引节点号=%lu\n",dirstruct->d_ino);
printf("偏移=%ld\n",dirstruct->d_off);
printf("求偏移量==%ld\n",size);
printf("类型=%d\n",dirstruct->d_type);
printf("名字=%d\n",dirstruct->d_reclen);
}
closedir(dir);
printf("关闭成功\n");
}
//测试偏移量打
void Test_Seek_and_tell(void)
{
int i =0;
long size;
struct dirent * dirstruct=NULL;
DIR * dir=NULL;
dir = opendir("./USER");
if(dir ==NULL)
{
perror("opendir");
return ;
}
printf("打开成功\n");
while(1)
{
if(i>=2)
{
size = telldir(dir);
break;
}
dirstruct = readdir(dir);
i++;
}
while(1)
{
dirstruct = readdir(dir);
if(dirstruct == NULL)
{
seekdir(dir,size);
sleep(2);
printf("*********\n");
continue;
}
printf("名字=%s\n",dirstruct->d_name);
printf("索引节点号=%lu\n",dirstruct->d_ino);
printf("偏移=%ld\n",dirstruct->d_off);
printf("类型=%d\n",dirstruct->d_type);
printf("名字=%d\n",dirstruct->d_reclen);
}
}