1、读取一个文件的元数据,文件可以从命令行输入,也可指直接以字符串传进。
(1)直接传字符串进去。也就是文件名。
#include<unistd.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
int main()
{
struct stat stat2;
char* type,*readok;
stat("a.c",&stat2);
if(S_ISREG(stat2.st_mode))
type="regular";
else if(S_ISDIR(stat2.st_mode))
type="directory";
else
type="other";
if(stat2.st_mode&S_IRUSR)
readok="yes";
else
readok="No";
printf("type:%s,read:%s\n",type,readok);
exit(0);
}
(2)从命令行传文件进去。
#include<unistd.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
struct stat stat2;
char* type,*readok;
stat(argv[1],&stat2);
if(S_ISREG(stat2.st_mode))
type="regular";
else if(S_ISDIR(stat2.st_mode))
type="directory";
else
type="other";
if(stat2.st_mode&S_IRUSR)
readok="yes";
else
readok="No";
printf("type:%s,read:%s\n",type,readok);
exit(0);
}