您的位置:首页 > 运维架构 > Linux

linux实验报告二--文件操作(续)

2006-03-09 10:55 417 查看
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h> // stat 函数所在的文件
#include <dirent.h>
int main(void)
{
DIR *dp;
struct dirent *ep;
struct stat st;
char dirp[50];
printf("Please input a dir name:/n");
scanf("%s",&dirp); //读入目录名
dp=opendir("dirp"); //打开所给目录
printf("filename:/ttype:/tPermission/taccesstime/tlastmodtime/tsize/t");
if(dp!=NULL) //如果打开目录成功,则进行操作。
{
while(ep = readdir(dp)) //读每一个目录项的循环
{
if(ep->d_name[0]!='.') //判断文件名称的第一个字符是否'.',如果是,表明是隐含文件,我们不动,否则操作
{
//用stat函数打开文件的信息,第一个参数是文件的路径,第二个参数存放文件的信息
if(stat(ep->d_name,&st)!=-1) //读成功
{
printf("%s/t",ep->d_name);
if((st.st_mode&S_IFMT)==S_IFDIR) //判断文件的类型
printf("Directory/t"); //目录
else if((st.st_mode&S_IFMT)==S_IFBLK) //块文件
printf("Block special file/t");
else if((st.st_mode&S_IFMT)==S_IFCHR) //特殊字符文件
printf("character special file/t");
else if((st.st_mode&S_IFMT)==S_IFREG) //普通文件
printf("Ordinary file/t");
else if((st.st_mode&S_IFMT)==S_IFIFO) //管道文件
printf("pipefile file/t");
printf("%o/t",st.st_mode&0x1ff); //文件的权限
printf("%15s/t",ctime(st.st_atime)); //文件创建时间
printf("%15s/t",ctime(st.st_mtime)); //文件上次修改时间
printf("%ld/n",st.st_size); //文件的大小
}
}
}
closedir(dp) //关闭目录

}
else
puts("Couldn't open the directory./n"); //打开不成功时,输出不能打开路径
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: