转换函数
fileno
fileno FILE*fp -> int fd
作用:获得一个文件流指针中的文件描述符参数:
stream:文件流指针返回值:
返回值:成功返回文件描述符失败返回-1
注意:1.内存模型大小一样
2.若没有特殊要求,建议关闭层级高的
fdopen
(权限必须对应)
功能:将文件描述符转化为文件流指针参数:
参数:fd:已经打开的文件描述符mode :
"w+"“r+”"w""r""a""a+"
返回值:成功返回文件流指针,失败返回NULL
目录相关操作
1.opendir() dir* ->整个目录 系统中称之为目录入口 目录流指针
功能:打开目录
参数:要打开的文件
返回值:失败NULL
成功返回目录流指针
2.readdir 需要循环读取 info->目录当中的一项
功能:读取目录
参数:目录流指针
返回值:
成功返回:结构体:系统还提供了状态信息
出错或者读到目录流末尾返回NULL
字符设备:读一写一,数据吞吐量小。有顺序要求
块设备:设备数据读取中,对顺序没有要求
3.closedir
功能 :关闭目录
参数 :opendir的返回结果中目录流对象
返回值:成功 0 失败 -1;
对于任何一个目录,都会有. (本层)..(上层)
命令级函数
1.chdir -> cd
int chdir(const char *path);// /home/linux
功能:改变当前程序的工作路径
参数:path:改变到的路径
返回值:成功返回0 失败返回-1
2.getcwd -> pwd
char *getcwd(char *buf, size_t size);
功能:获得当前的工作路径
参数:buf:保存工作路径空间的首地址 size:保存路径空间的长度
返回值:成功返回包含路径空间的字符串首地址成功返回将指向buf 失败返回NULL
3.mkdir ->mkdir
mkdir(新目录文件名(区分大小写),777(umask新建文件或者目录时,系统默认会和该数字相减))
int mkdir(const char *pathname, mode_t mode);//777 666 --x--x--x
功能:创建一个目录666-
参数:pathname:路径 mode: mode & ~umask 0002
对目录来说,x是切换进和切换出的权限
返回值:成功返回0 失败返回-1
4.rmdir ->rm -fr
int rmdir(const char *pathname);
功能:删除一个空目录文件(必须为空)
参数: pathname:目录文件的名字
返回值: 成功返回0 失败返回-1
stat
stat (文件名/路径名,结构体指针)
功能:获得文件属性
返回值:1.成功返回结构体
2.失败返回-1;
Inode :文件位置编号
mode:文件类型和权限
link:硬链接数
stat mode man 7 inode
套入宏,只命中一个,(本身为32bit的数,其中7个为文件类型,只有一位被置1)
#include<stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <dirent.h>
#include<unistd.h>
#include <sys/stat.h>
#if 0
int main(int argc,char *argv[])
{
DIR *dir = opendir("./");
if (dir == NULL)
{
printf("error\n");
return 1;
}
while(1)
{
struct dirent *info = readdir(dir);
if(info == NULL)
{
break;
}
switch(info -> d_type)
{
case DT_BLK:
printf("块设备 ");
break;
case DT_DIR:
printf("目录文件 ");
break;
case DT_CHR:
printf("字符文件 ");
break;
case DT_REG:
printf("普通文件 ");
break;
default:
printf("其他文件 ");
}
printf("%s\n",info -> d_name);
}
closedir(dir);
return 0;
}
int main(void)
{
FILE *fp = fopen("1.txt","r");
if(fp == NULL)
{
printf("fopen error\n");
return 1;
}
char buf[256] = {0};
int fd = fileno(fp);
read(fd,buf,sizeof(buf) - 1);
printf("%s\n",buf);
fclose(fp);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
//FILE*---> int fd;
int fd = open("1.txt",O_RDONLY);
if(-1 == fd)
{
printf("fopen error\n");
return 1;
}
char buf[256]={0};
FILE* fp = fdopen(fd,"r");
if(NULL == fp)
{
printf("fdopen error\n");
return 1;
}
fgets(buf,sizeof(buf),fp);
printf("%s\n",buf);
fclose(fp);
return 0;
}
int main()
{
int ret = chdir("../");
if(ret == -1)
{
printf("error chdir");
return 1;
}
fopen("aaa","w");
return 0;
}
int main()
{
char buf[512] = {0};
getcwd(buf,sizeof(buf));
printf("current path is %s\n",buf);
int ret = chdir("../");
if(ret == -1)
{
printf("error\n");
return 1;
}
getcwd(buf,sizeof(buf));
printf("change after path is%s\n",buf);
return 0;
}
int main()
{
int ret = mkdir("b",0777);
if(ret == -1)
{
printf("mkdir error\n");
return 1;
}
return 0;
}
int main()
{
int ret = rmdir("b");
if(ret == -1)
{
printf("rmdir error\n");
return 1;
}
return 0;
}
int main(int argc,char *argv[])
{
struct stat st;
int ret = stat("1.txt",&st);
if(ret == -1)
{
printf("stat error");
return 1;
}
printf("ino:%lu mode:%d link:%lu uid:%d gid:%d size:%lu time:%lu\n",st.st_ino, st.st_mode,st.st_nlink,st.st_uid, st.st_gid,st.st_size,st.st_mtim.tv_sec);
return 0;
#endif
int main(int argc,char *argv[])
{
struct stat st;
char filename[] = "1.txt";
int ret = stat(filename,&st);
if(ret == -1)
{
printf("stat error");
return 1;
}
if(S_ISREG(st.st_mode))
{
fputc('-',stdout);
}
else if(S_ISDIR(st.st_mode))
{
fputc('d',stdout);
}
else if(S_ISCHR(st.st_mode))
{
fputc('c',stdout);
}
else
{
fputc('o',stdout);
}
//-rw-r--r-- 1 linux linux 29 Aug 7 11:28 1.txt
if(st.st_mode & S_IRUSR )
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IWUSR )
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IXUSR )
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IROTH )
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IWOTH )
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IXOTH )
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IRGRP )
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IWGRP )
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode & S_IXGRP )
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
printf(" %lu %d %d %lu %lu %s\n",st.st_nlink,st.st_uid,st.st_gid,st.st_size
,st.st_mtim.tv_sec,filename) ;
return 0;
}