1.fileno函数和fdopen函数:文件类型转换
//fileno
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
//FILE*---> int fd;
FILE* fp = fopen("1.txt","r");
if(NULL == fp)
{
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;
}
//fdopen
#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;
}
2.ls函数:使用opendir,readdir函数获得目录下文件
//基本
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dir = opendir("./");
if(NULL == dir)
{
printf("opendir");
return 1;
}
while(1)
{
struct dirent * info = readdir(dir);
if(NULL == info)
{
break;
}
printf("%s\n",info->d_name);
}
closedir(dir);
return 0;
}
//更详细
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dir = opendir("./");
if(NULL == dir)
{
printf("opendir");
return 1;
}
while(1)
{
struct dirent * info = readdir(dir);
if(NULL == info)
{
break;
}
switch(info->d_type)
{
case DT_DIR:
printf("目录文件 ");
break;
case DT_BLK:
printf("块设备 ");
break;
case DT_CHR:
printf("字符文件 ");
break;
case DT_REG:
printf("普通文件 ");
break;
default:
printf("其他文件 ");
}
printf("%s\n",info->d_name);
}
closedir(dir);
return 0;
}
3.chdir函数:改变目录,创建文件
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int ret = chdir("../");
if(-1 == ret)
{
printf("chdir error\n");
return 1;
}
fopen("aaa","w");
return 0;
}
4.getcwd函数:获得目录的路径
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char buf[512]={0};
getcwd(buf,sizeof(buf));
printf("current path is %s\n",buf);
int ret = chdir("../");
if(-1 == ret)
{
printf("chdir error\n");
return 1;
}
getcwd(buf,sizeof(buf));
printf("after change path is %s\n",buf);
return 0;
}
5.mkdir函数:创建文件
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
int ret = mkdir("bbb",0777);
if(-1 == ret)
{
printf("mkdir error\n");
return 1;
}
return 0;
}
6.rmdir函数:删除文件
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int ret = rmdir("bbb");
if(-1 == ret)
{
printf("rmdir error\n");
return 1;
}
return 0;
}
7.stat函数:获得文件的具体信息
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct stat st;
int ret = stat("./01ls.c",&st);
if(-1 == ret)
{
printf("stat error\n");
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;
}
8.ll 函数:获得文件权限
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct stat st;
char filename[]="./01ls.c";
int ret = stat(filename,&st);
if(-1 == ret)
{
printf("stat error\n");
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);
*/
// -rw-rw-r-- 1 linux linux 433 Aug 7 11:11 01ls.c
// chr c blk b fifo p sym l sock s
// man 7 inode 169line
if( S_ISREG(st.st_mode))
{
fputc('-',stdout);
}
else if(S_ISDIR(st.st_mode))
{
fputc('-',stdout);
}
else if(S_ISCHR(st.st_mode))
{
fputc('c',stdout);
}
else
{
//other
fputc('o',stdout);
}
// -rw-rw-r-- 1 linux linux 433 Aug 7 11:11 01ls.c
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);
}
// -rw-rw-r-- 1 linux linux 433 Aug 7 11:11 01ls.c
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;
}