首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C/C++怎么获得文件属性

2012-02-15 
C/C++如何获得文件属性?rt[解决办法]http://topic.csdn.net/t/20021127/11/1209086.htmlhttp://topic.csdn

C/C++如何获得文件属性?
rt

[解决办法]
http://topic.csdn.net/t/20021127/11/1209086.html
http://topic.csdn.net/t/20020613/19/801649.html
http://topic.csdn.net/t/20020918/09/1032607.html
[解决办法]
函数名: stat
功 能: 读取打开文件信息
用 法: int stat(char *pathname, struct stat *buff);
程序例:

#include <sys\stat.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: %c\n ", 'A '+statbuf.st_dev);
printf( "Size of file in bytes: %ld\n ", statbuf.st_size);
printf( "Time file last opened: %s\n ", ctime(&statbuf.st_ctime));
return 0;
}
[解决办法]
这样也可以

win32_find_data filestruct;
handle hf;
hf = findfirstfile(文件名.c_str(), &filestruct);

filestruct.ftcreationtime//创建时间
filestruct.ftlastaccesstime//访问时间
filestruct.ftlastwritetime//修改时间

findclose(hf);
[解决办法]
unix 系统下:

【Ref:http://www.tongyi.net/os/unix/1055532.html】
如何在程序中获得文件的属性。这里需要用到三个函数,它们的定义是:
#include <sys/types.h>
#include <sys/stat.h>

int stat(const char* pathname, struct stat* buf); //成功则返回0,失败返回-1
int fstat(int fd, struct stat* buf);     //成功则返回0,失败返回-1
int lstat(const char* pathname, struct stat* buf); //成功则返回0,失败返回-1

  stat()函数接收一个文件名,返回该文件的stat结构。fstat()接收一个已打开的文件描述符,返回该文件的stat结构。lstat()与stat()类似,但对于符号链接,它返回该符号链接的stat结构,而不是该符号链接所引用文件的stat结构。stat结构是一包含了文件所有属性信息的结构,它的定义如下:

struct stat{
mode_t st_mode; //file type & mode
ino_t st_ino; //i-node number
dev_t st_dev; //device number
dev_t st_rdev; //device number for special files
nlink_t st_nlink; //number of links
uid_t st_uid; //user ID of owner
gid_t st_gid; //group ID of owner
off_t st_size; //size in bytes,for regular files
time_t st_atime; //time of last access
time_t st_mtime; //time of last modification
time_t st_ctime; //time of last file status change
long st_blksize;//best I/O block size
long st_blocks; //number of 512 bytes blocks allocated
};

  stat结构中最常用到的属性是st_mode(文件的类型及文件的访问权限)、st_nlink(硬链接数,表示有几个链接到该文件上)、st_uid、st_gid、st_size(以字节为单位的文件长度,只对普通文件、目录文件和符号连接有意义)、st_atime、st_mtime、st_ctime。


我们曾一再提到Unix系统中一切皆可视为文件,不过细而化之的话又可以分为多种类型,那么在程序中如何去对文件类型进行判别呢?这就需要用到下表中所示的一些宏:

宏 作用
S_ISREG() 测试是否为普通文件
S_ISDIR() 测试是否为目录文件
S_ISCHR() 测试是否为字符特殊文件
S_ISBLK() 测试是否为块特殊文件
S_ISFIFO() 测试是否为FIFO文件
S_ISLNK() 测试是否为链接文件
S_ISSOCK() 测试是否为socket文件
S_ISUID() 测试是否设置了“设置-用户-ID”位
S_ISGID() 测试是否设置了“设置-组-ID”位


  [程序6]

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

  int main(int argc, char * argv[])
{
    int i;
    struct stat buf;
    char *ptr;
    for(i=1; i < argc; i++)
    {
      printf( "%s: ", argv[i]);
      if(lstat(argv[i],&buf) <0)
      {
  printf( "lstat error. ");
  continue;
      }
      if(S_ISREG(buf.st_mode)) ptr = "regular ";
      else if(S_ISDIR(buf.st_mode)) ptr = "directory ";
      else if(S_ISCHR(buf.st_mode)) ptr = "char special ";
      else if(S_ISBLK(buf.st_mode)) ptr = "block special ";
      else if(S_ISFIFO(buf.st_mode)) ptr = "fifo ";
      else ptr = "Others ";
      printf( " %s\n ",ptr );
    }
    return 0;
  }

  编译程序后,我们先来使用命令mkfifo myfifo.pipe建一个管道文件,然后运行:
filetype myfifo.pipe . /etc/passwd
屏幕会显示:
myfifo.pipe : fifo
 . : directory
 /etc/passwd : regular

  此外,st_mode属性还包含了可用于对文件的属主及权限进行判断的宏,如下表所示:

宏 意义
S_IRUSR 所有者-读
S_IWUSR 所有者-写
S_IXUSR 所有者-执行
S_IRGRP 组-读
S_IWGRP 组-写
S_IXGRP 组-执行
S_IROTH 其他-读
S_IWOTH 其他-写
S_IXOTH 其他-执行


  [程序7]

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int main()
{
cout < < " enter a file name: ";
char name[255];
cin > > name;
struct stat buf;
if(lstat(name,&buf) <0)
{
cout < < " file not existing. " < < endl;
return -1;
}
if(buf.st_mode & S_IRUSR) cout < < " user can read| ";
if(buf.st_mode & S_IWUSR) cout < < " user can write| ";
if(buf.st_mode & S_IXUSR) cout < < " user can execute| ";

if(buf.st_mode & S_IRGRP) cout < < " group can read| ";
if(buf.st_mode & S_IWGRP) cout < < " group can write| ";
if(buf.st_mode & S_IXGRP) cout < < " group can execute| ";

if(buf.st_mode & S_IROTH) cout < < " other can read| ";
if(buf.st_mode & S_IWOTH) cout < < " other can write| ";
if(buf.st_mode & S_IXOTH) cout < < " ohter can execute| ";
cout < < endl;
return 0;
}

如果是想判断某用户对某一文件的访问权限,则可调用access()函数,它的定义是:
access(const char* path, int amode)
其中,amode对应的值有R_OK(可读)、W_OK(可写)、X_OK(可执行)、F_OK(文件存在)。

热点排行