文件和目录

 

一、获取当前目录


#include <unistd.h>

char *getcwd(char *buffer,size_t size)

参数说明:buffer存放当前工作目录的指针,size为buffer的大小;

返回值:调用成功,返回指向当前工作目录的字符串,失败,返回NULL并设置errno

 

 

二、获得系统目录的最大长度


#include <unistd.h>

long pathconf(char *path,int name);

参数说明:path指明要获取信息的目录,name指明要获取什么信息

返回值:调用成功,返回需要的信息,否则,返回-1,并设置errno的值

其中name可为:

_PC_LINK_MAX  返回文件或是目录的最大链接数;

_PC_MAX_CANON  path如果指向终端,则返回格式化输出最大长度

_PC_MAX_INPUT     path如果指向终端,则返回格式化输入最大长度

_PC_NAME_MAX   返回创建进程时允许的最大文件名长度

_PC_PATH_MAX   返回当前工作目录的最大长度

 

三、更改当前的工作目录


#include <unistd.h>

int chdir (const char *path)

int fchdir (int fd)

参数说明: path 指向更改之后的目录,fd是目录的文件描述符

返回值:调用成功返回0否则返回-1并设置errno

 

实例

#include <unistd.h>
#include <iostream>
#include <stdlib.h>

int main(void){
   long cur_path_len;
   char* cur_work_dir;
  
   /*获得目录最大长度*/
   if((cur_path_len=pathconf(".",_PC_PATH_MAX))==-1){
      perror("Couldn't get current working path length");
      return 1;
   }

  std::cout<<"Current Path Length Is "<<cur_path_len<<std::endl;

  /*根据获得的目录最大长度,分配内存*/
  if((cur_work_dir=(char *)malloc(cur_path_len))==NULL){
      perror("Couldn't allocate memory for the pathname");
      return 1;
  }

  if(getcwd(cur_work_dir,cur_path_len)==NULL){
      perror("Couldn't get current working directory");
      return 1;
  }

  std::cout<<"Current Working Directory is"<<cur_work_dir<<std::endl;
  
  /*更改当前工作目录到上级目录*/
  if(chdir("/home/")==-1){
     perror("Couldn't change current working directory");
     return 1;
  }

  if(getcwd(cur_work_dir,cur_path_len)==NULL){
      perror("Couldn't get current working directory");
      return 1;
  }

  std::cout<<"After chdir function call,Current Working Directory is"<<cur_work_dir<<std::endl;

  return 0;
}


四、创建和删除目录


    创建目录

#include <sys/types.h>

#include <sys/stat.h>

int mkdir(const char *pathname,mode_t mode)

参数说明:pathname是创建的目录的名字,mode是目录所具有的权限(mode&umask&0777)

返回值:调用成功返回0,否则,返回-1,并设置errno

 

   删除目录

#include <unistd.h>

int mkdir(const char *path)

参数说明:path为要删除的文件的指针

返回值:调用成功,返回0,否则,返回-1,并设置errno的值

 

实例

#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
#include <stdlib.h>

int main(void){
   char pathname[] ="/home/shisir/chapter4/a";
  
   /*mode 设置为0700,  开始的0表示八进制*/
   if(mkdir(pathname,0700)==-1){
      perror("Couldn't create the directory");
      return 1;
   }

   std::cout<<"pree any key to continue.../n";
   getchar();

  if (rmdir(pathname) == -1)
  {
      perror("Could not remove the directory!/n");
      return 1;
  }     

  std::cout<<"press any key to continue.../n";
  getchar();
   return 0;
}

 

其间,可以输入ls查看目录是否创建成功或是删除成功。

 

五、获取文件信息

 

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

int stat(const char *path,struct stat *buf);

int fsat(int fd,struct stat *buf);

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

功能说明:把path指明的文件的相关信息放在buf中

参数说明:path(fd)需要获取信息的文件名,buf信息存放地址

返回值:成功返回0,否则,返回-1,并设置errno

 

这里顺便介绍一下stat结构:

struct stat

{

dev_t  st_dev;     //   文件所处的磁盘的设备ID号

ino_t   st_ino;      //    inode的索引号

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;    // 文件系统的块的大小

blkcnt_t st_blocks;    

time_t  st_atime;        // 文件最后访问的时间

time_t st_ctime;         // 文件属性的最后修改时间

time_t st_mtime;        // 文件的最后修改时间

};

 

实例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

int main(int argc,char* argv[]){
    struct stat file_stat;
   
    if(argc!=2){
        printf("Usage:%s filename/n",argv[0]);
        return 1;
    }

    if(stat(argv[1],&file_stat)==-1){
        perror("Cannot get the information of the file!/n");
        return 1;
    }

    if(S_ISREG(file_stat.st_mode))
        printf("%s is Regular File,Judged by S_ISREG/n",argv[1]);
       

    if(file_stat.st_mode & S_IFREG)
        printf("%s is Regular File,Judeged by bits calculate S_IFREG/n",argv[1]);

    if(S_ISDIR(file_stat.st_mode))
        printf("%s is Directory,Judged by S_ISDIR/n",argv[1]);

    if(file_stat.st_mode & S_IFDIR)
        printf("%s is Directory,Judged by bit calculate S_ISDIR/n",argv[1]);

   
    printf("Owner ID: %d, Group ID: %d/n",file_stat.st_uid,file_stat.st_gid);
        printf("Permission: %o/n",file_stat.st_mode & 0x1ff);
    printf("Last Access Time: %15s/n",ctime(&file_stat.st_atime));
    printf("Last Modification Time: %15s/n",ctime(&file_stat.st_mtime));
    printf("Last Status Change Time: %15s/n",ctime(&file_stat.st_ctime));
   
    return 0;
}

 

六、修改我文件权限

 

#include <sys/types.h>

#include <sys/stat.h>

int chmod(const char *path,mode_t mode);

int fchmod(int fd,mode_t mode);

功能说明:修改path指定的文件的权限为mode

参数说明:path为需修改的文件名,mode为修改后的文件权限

返回值:成功返回0,否则返回-1,并设置errno

 

实例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc,char* argv[]){
    if(argc!=2){
        printf("Usage:%s filename/n",argv[0]);
        return 1;
    }

    if(chmod(argv[1],S_IRUSR | S_IRGRP | S_IXOTH | S_IROTH)<0){
        perror("Cannot modify the Permission of the file");
        return 1;
    }

    return 0;
}

 

七、修改文件拥有者

 

#include <sys/types.h>

#include <unistd.h>

int chown(const char *path,uid_t owner,gid_t group);

int fchown(int fd,uid_t owner,gid_t group);

int lchown(const char *path,uid_t owner,gid_t group);

功能说明:修改path的拥有者和拥有者所在的组

参数说明:path(fd)为文件名(文件描述符),owner为文件拥有者,group为文件拥有者所在的组

返回值:成功返回0,否则,返回-1,并设置errno

 

八、umask函数

 

#include<sys/types.h>

#include <sys/stat.h>

mode_t umask(mode_t mask);

功能说明:更改mask的值

参数说明:mask为更改之后mask的值

返回值:返回先前的mask值

实例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void){
        int fd1,fd2;
       
        fd1=open("test",O_CREAT | O_RDWR,0777);   
    
    if(fd1<0){
        perror("Cannot create the test file");
        return 1;
    }
   
     close(fd1);

        struct stat file_stat;
   
    if(stat("test",&file_stat)==-1){
        perror("Cannot get the information of the file!/n");
        return 1;
    }

    printf("Premission is : %o/n",file_stat.st_mode & 0x1ff);

    umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
   
     fd2=open("test1",O_CREAT | O_RDWR,0777);

    if(fd2<0){
         perror("Cannot create the test file");
          return 1;
    }
   
    close(fd1);

     if(stat("test1",&file_stat)==-1){
         perror("Cannot get the information of the file!/n");
         return 1;
     }      

     printf("After Modify umask value, Premission is : %o/n",file_stat.st_mode & 0x1ff);

    return 0;
}


十、链接函数

 

#include <unistd.h>

int link(const char *oldpath,const char * newpath);

int symlink(const char *oldpath,const char *newpath);

int unlink(const char *pathname);

功能说明:link建立从newpath指向oldpath的硬链接;symlink建立从newpath到oldpath的符号链接 unlink删除指定的链接

参数说明:oldpath是系统中原有的文件,newpath是建立链接时给出的文件。

返回值:成功返回0,否则返回-1,并设置errno

实例

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    if (argc != 3)
    {
        printf("Usage :%s oldpath,newpath/n",argv[0]);
        return 1;
    }

    if ( creat(argv[1],0644) == -1)
    {
        printf("Failed to create the file!/n");
        return 1;
    }

    if (link(argv[1],argv[2]) == -1)
    {
        perror("Failed to create the link ./n");
        return 1;
    }

    printf("press any key to continue..../n");
    getchar();

    printf("now delete the link file./n");
    unlink(argv[1]);
    printf("press any key to continue.../n");
        getchar();
}

 



 


 

 

 

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值