C语言函数—_status87

函数名

stat

功 能

读取打开文件信息

用 法

int stat(char *pathname, struct stat *buff);

程序例

#include <sysstat.h> 
#include <stdio.h> 
#include <time.h> 

#define FILENAME "TEST.$$$" 

int main(void) 
{ 
   struct stat statbuf; 
   FILE *stream; 

   /* open a file for update */ 
   if ((stream = fopen(FILENAME, "w+")) == NULL) 
   { 
      fprintf(stderr, "Cannot open output file.n"); 
      return(1); 
   } 

   /* get information about the file */ 
   stat(FILENAME, &statbuf); 

   fclose(stream); 

   /* display the information returned */ 
   if (statbuf.st_mode & S_IFCHR) 
      printf("Handle refers to a device.n"); 
   if (statbuf.st_mode & S_IFREG) 
      printf("Handle refers to an ordinary file.n"); 
   if (statbuf.st_mode & S_IREAD) 
      printf("User has read permission on file.n"); 
   if (statbuf.st_mode & S_IWRITE) 
      printf("User has write permission on file.n"); 

   printf("Drive letter of file: %cn", 'A'+statbuf.st_dev); 
   printf("Size of file in bytes: %ldn", statbuf.st_size); 
   printf("Time file last opened: %sn", ctime(&statbuf.st_ctime)); 
   return 0; 
} 
  
  
  

如果引用本站的原创文章,请注明原文链接:,本站保留追究责任的权利!