分类: C/C++
2009-11-10 13:11:23
一、设计一个程序,要求判断“/etc/passwd”的文件类型。
使用st_mode属性,可以使用几个宏来判断:
S_ISLNK(st_mode)是否是一个连接,S_ISREG是否是一个常规文件,
S_ISDIR是否是一个目录,S_ISFIFO是否是一个FIFO文件,S_ISSOCK
是否是一个SOCKET文件;
并且要求打开文件“/etc/passwd”,判断它的最后一次访问时间。
#include
#include
#include
#include
#include
void what_is( int);
int main()
{
struct stat info;
if( stat("/etc/passwd", &info) == -1 )
perror("etc/passwd");
else
what_is( info.st_mode );
printf("last access time is %s\n", ctime(&info.st_mtime)); //以字符串的形式打印时间;
return 0;
}
void what_is( int mode )
{
if( S_ISDIR(mode) ) printf("/etc/passwd is Directory file.\n"); //stat_mode返回的是条件中的字符串;
if( S_ISFIFO(mode) ) printf("/etc/passwd is Fifo file.\n");
if( S_ISLNK(mode) ) printf("/etc/passwd is link file.\n");
if( S_ISREG(mode) ) printf("/etc/passwd is regular file.\n");
if( S_ISSOCK(mode) ) printf("/etc/passwd is socket file.\n");
}