文件IO操作(08)——stat函数(2)

fstat函数输出三个标准文件流的stat信息

STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO的信息

 

   1: #include <sys/types.h>
   2: #include <sys/stat.h>
   3: #include <unistd.h>
   4: #include <stdio.h>
   5: #include <stdlib.h>
   6: #include <string.h>
   7: #include <errno.h>
   8: #include <time.h>
   9:     
  10: //       int stat(const char *path, struct stat *buf);
  11:  
  12: void fstat_show(int fd)
  13: {
  14:  
  15:     struct stat buf;
  16:     if(0==fstat(fd,&buf)){
  17:         printf("文件fd:\"%d\"\n",fd);
  18:         printf("大小:%ld\t\t块:%ld\t\tIO块:%ld\n设备:%ldh/%ldd\t\tInode:%ld\t\t硬链接:%ld\n",
  19:             buf.st_size,buf.st_blocks,buf.st_blksize,0,buf.st_dev,
  20:             buf.st_ino,buf.st_nlink);
  21:         printf("权限:(0%o/",buf.st_mode & 0777);
  22:         if(buf.st_mode & S_IFDIR){
  23:             printf("d");
  24:         }else{
  25:             printf("-");
  26:         }
  27:         if(buf.st_mode & S_IRUSR){
  28:             printf("r");
  29:         }else{
  30:             printf("-");
  31:         }
  32:         if(buf.st_mode & S_IWUSR){
  33:             printf("w");
  34:         }else{
  35:             printf("-");
  36:         }
  37:         if(buf.st_mode & S_IXUSR){
  38:             printf("x");
  39:         }else{
  40:             printf("-");
  41:         }
  42:         if(buf.st_mode & S_IRGRP){
  43:             printf("r");
  44:         }else{
  45:             printf("-");
  46:         }
  47:         if(buf.st_mode & S_IWGRP){
  48:             printf("w");
  49:         }else{
  50:             printf("-");
  51:         }
  52:         if(buf.st_mode & S_IXGRP){
  53:             printf("x");
  54:         }else{
  55:             printf("-");
  56:         }
  57:         if(buf.st_mode & S_IROTH){
  58:             printf("r");
  59:         }else{
  60:             printf("-");
  61:         }
  62:         if(buf.st_mode & S_IWOTH){
  63:             printf("w");
  64:         }else{
  65:             printf("-");
  66:         }
  67:         if(buf.st_mode & S_IXOTH){
  68:             printf("x");
  69:         }else{
  70:             printf("-");
  71:         }
  72:  
  73:         printf(")\tUid:( %ld )\tGid:( %ld )\n",buf.st_uid,buf.st_gid);
  74:         struct tm tm_tmp;
  75:         localtime_r(&(buf.st_atime),&tm_tmp);
  76:         printf("最近访问:%s",asctime(&tm_tmp));
  77:         
  78:         localtime_r(&(buf.st_mtime),&tm_tmp);
  79:         printf("最近更改:%s",asctime(&tm_tmp));
  80:  
  81:         localtime_r(&(buf.st_ctime),&tm_tmp);
  82:         printf("最近改动:%s",asctime(&tm_tmp));
  83:  
  84:  
  85:     }else{
  86:         perror("error ");
  87:     }
  88: }
  89:  
  90:  
  91: //===========================================================
  92:  
  93:  
  94: int main(int argc,char* argv[])
  95: {
  96:     fstat_show(STDIN_FILENO);   
  97:     fstat_show(STDOUT_FILENO);  
  98:     fstat_show(STDERR_FILENO);  
  99:  
 100:     return 0;
 101: }

调用system函数来输出

 

   1: #include <stdlib.h>
   2: #include <sys/types.h>
   3: #include <sys/stat.h>
   4: #include <unistd.h>
   5:  
   6: int main()
   7: {
   8:     puts("STDIN_FILENO");
   9:     //int mypid=getpid();
  10:     //char cmd[256]={0};
  11:     
  12:     system("stat /dev/pts/1");
  13:  
  14:     //puts("STDOUT_FILENO");
  15:     //puts("STDERR_FILENO");
  16:     return 0;
  17: }

你可能感兴趣的:(文件IO操作(08)——stat函数(2))