文件操作相关函数

stat/lstat函数

函数描述:获取文件属性

函数原型:int stat(const char* pathname,struct stat *buf);

                  int lstat(const char* pathname,struct stat *buf);

返回值 0 OR 1

struct stat:

 stat简单练习:
 

  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
  #include<sys/types.h>
  #include<unistd.h>
  #include<sys/stat.h>
  #include<fcntl.h>
  
  int main(int argc, char *argv[])
  {
  
 
     struct stat buf;  
     //int stat(const char* pathname,struct stat *buf);
     int n = stat(argv[1],&buf);
     printf("size:[%d],uid:[%d],gid:[%d]\n",buf.st_size,buf.st_uid,buf.st_gid);
  
     return 0;

 }
             

st_mode属性讲解:

后面所属用户,所属组,其他人的权限判断,只需要将  st_mode&相应的掩码 即可判断

 用法:

if(st_mode & S_IRUSR)    //所有者可读
if(st_mode & S_IXGRU)    //所属组可执行
if(st_mode & S_IWOTH)    //其他人可写

而文件类型的判断的操作稍微复杂

 lstat与stat的区别

        1.对于普通文件来说,lstat函数与stat函数一样

        2.对于软连接来说,lstat函数获取的是连接文件本身的属性

                                        stat函数获取的是连接文件指向的文件的属性。

目录遍历函数

man opendir

 man readdir

 

 练习:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<string.h>
  4 #include<sys/types.h>
  5 #include<unistd.h>
  6 #include<dirent.h>
  7 
  8 int main(int argc,char *argv[])
  9 {
 10 
 11     //DIR *opendir(const char *name);
 12     //打开目录
 13     DIR *pDir = opendir(argv[1]);
 14     if(pDir==NULL)
 15     {
 16         perror("opendir error");
 17         return -1;
 18     }
 19 
 20     //循环读取目录项
 21     //struct dirent *readdir(DIR *dirp);     
 22     struct dirent *pDent=NULL;
 23     while((pDent=readdir(pDir))!=NULL)
 24     {
 25         printf("[%s]\n",pDent->d_name);
 26 
 27         //判断文件类型
 28         switch(pDent->d_type)
 29         {
 30             case DT_REG:
 31                 printf("普通");
 32                 break;
 33             case DT_DIR:
 34                 printf("目录");
 35                 break;
 36 
 37             case DT_LNK:
 38                 printf("链接");
 39                 break;
 40         }
 41         printf("\n");
 42 
 43     }
 44 
 45     //关闭目录
 46     closedir(pDir);
 47     return 0;
 48 
 49 
 50 }

dup函数详解:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<sys/types.h>
  5 #include<unist.d>
  6 #include<sys/stat.h>
  7 #include<fcntl.h>
  8 
  9 int main(int argc,char* argv[])
 10 {
 11     //打开文件
 12     int fd = open(argv[1],O_RDWR);
 13     if(fd<0)
 14     {
 15         perroe("open error");
 16         return -1;
 17     }
 18 
 19     //调用dup函数复制fd
 20 
 21     int newfd = dup(fd);
 22     printf("newfd:[%d],fd:[%d]\n",newfd,fd);
 23 
 24     //使用fd对文件进行写操作
 25     write(fd,"hello world",strlen("hello world"));
 26 
 27     //调用lseek函数移动文件指针到开始出
 28     lseek(fd,0,SEEK_SET);
 29     
 30     //使用newfd读文件
 31     char buf[64];
 32     memset(buf,0x00,sizeof(buf));   
 33     int n = read(newfd,buf,sizeof(buf));
 34     printf("read over:n==[%d], buf==[%s]\n",n.buf);
 35     
 36     //关闭文件
 37     close(fd);
 38     close(newfd);
 39 }

int dup2(int oldfd,int newfd);

        优势:可以指定newfd,而dup中的newfd是内核返回的  


    dup2()
       *  If oldfd is not a valid file descriptor, then the call fails, and newfd is
          not closed.

       *  If  oldfd  is  a  valid  file  descriptor, and newfd has the same value as
          oldfd, then dup2() does nothing, and returns newfd.

利用dup2实现文件重定向

  1 //利用dup2函数实现标准输出重定向
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<string.h>
  5 #include<sys/types.h>
  6 #include<unistd.h>
  7 #include<sys/types.h>
  8 #include<sys/stat.h>
  9 #include<fcntl.h>
 10 int main(int argc,char *argv[])
 11 {
 12     //打开文件
 13     int fd = open(argv[1],O_RDWR | O_CREAT,0777);
 14     if(fd<0)
 15     {
 16         perror("open error");
 17         return -1;
 18     }
 19     //调用dup2函数实现文件重定向操作
 20     dup2(fd,STDOUT_FILENO);
 21 
 22     printf("666666666666");
 23     close(fd);close(STDOUT_FILENO);
        return 0;
 24 
 25 }
~                                                                                                                
~                                                                                                                
~                                                                                                                
~                                    

fcntl函数

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值