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;
}
#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;
}
相关文章推荐
- linux实验报告二--文件操作
- 操作系统大型实验进展(6)-----Linux 内核文件系统与设备操作流程分析
- Linux实验报告-源代码编译安装Apache(Tarball文件安装)
- x4412开发板&ibox卡片电脑项目实战19-linux文件操作实验
- x4412开发板&ibox卡片电脑项目实战19-linux文件操作实验
- linux___FILE__文件操作
- linux中关于文件系统的简要介绍和一般操作
- 【Linux常用指令整理1】文件操作
- linux编程-文件操作(1)
- Linux Oracle dbf文件误删 恢复操作
- 一次对数据库实例、数据库启动、参数文件的操作实验
- Linux之文件操作(1)
- Linux - 文件基本操作管理
- Linux目录结构及文件基本操作
- 【Unix/Linux】【命令】【文件,目录操作工具】列出所有子目录与文件——ls (ll)命令
- 2017.7.18 linux下用户、组和文件的操作
- Linux下文本文件合并和去除重复操作
- Linux 学习--简单命令入门文件操作命令
- Linux 下的文件操作
- linux下关于文件操作记录