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

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

------解决方案--------------------
http://topic.****.net/t/20021127/11/1209086.html
http://topic.****.net/t/20020613/19/801649.html
http://topic.****.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。