文件操作

与时间有关的SHELL命令
与时间有关的shell命令:
date //显示当前日期-中国北京时间
date -U //显示当前日期时间 -世界标准时间UTC
date -R //显示当前日期时间 -RFC格式
cal显示日历 cal month year //显示指定年月的日历
time ./a.out //显示程序运行的时间 real 程序开始运行到结束的时间 user 用户cpu时间 sys 系统cpu时间
说明:用户cpu时间就是执行用户指令所用的时间
系统cpu时间就是该进程执行内核程序所经历的时间
时间编程:
GUN/Linux提供的时间获取api:
数据类型定义及结构体描述:
typedef long time_t;
struct tm{
int tm_sec; //秒 取值区间[0 ,59]
int tm_min; //分 取值范围[0 ,59]
int tm_hour;//时 取值范围[0 ,23]
int tm_mday;//一个月中的日期 取值范围[1,31]
int tm_mon;//月份(从一月开始,0代表一月) 取值区间为[0,11]
int tm_year;//年份 :其值等于实际年份加1900
int tm_wday; //星期 取值区间为[0,6],其中0代表星期天,1代表星期一
int tm_yday; //从每年的一月一日开始的天数,取值区间为[0,365],其中0代表一月一日,1代表一月2日

 };

获取日历时间 函数原型:time_t time(time_t *t)
函数功能:返回获取的日历时间
头文件:<time.h>
参数:time_t类型的指针变量或者填充NULL
返回值:成功返回日历时间,失败返回-1
获得日历时间是基础
获取格林威治时间 原型: struct tm *gmtime(const time_t *timep)
功能:将参数timep所指定的日历时间转换为标准时间
头文件:<time.h>
参数:timep待转换的日历时间
返回值:成功返回世界标准时间,以struct tm形式存储
例如:
#include <stdio.h>
#include <time.h>
int main(int argc,char *argv[])
{
time_t tim;
struct tm *g_tim;
tim=time(NULL);
g_tim=gmtime(&tim); //注意函数参数类型
printf("%d %d %d %d %d “,g_tim->tm_year+1900,g_tim->tm_mon+1,g_tim->tm_mday,g_tim->tm_hour,g_tim->tm_min);
}
获取本地时间: 原型:struct tm *localtime(const time_t *timep)
功能:将timep指向的日历时间转换为本地时间
头文件:<time.h>
参数:timep:指向待转换日历时间
返回值:返回struct tm形式的存储的本地时间 失败返回null
例如:
#include <stdio.h>
#include <time.h>
int main(int argc,char *argv[])
{
time_t tim;
struct tm *g_tim;
struct tm *b_tim;
tim=time(NULL);
g_tim=gmtime(&tim); //注意函数参数类型
b_tim=localtime(&tim);
printf(”%d %d %d %d %d \n",g_tim->tm_year+1900,g_tim->tm_mon+1,g_tim->tm_mday,g_tim->tm_hour,g_tim->tm_min);
printf("%d %d %d %d %d \n",b_tim->tm_year+1900,b_tim->tm_mon+1,b_tim->tm_mday,b_tim->tm_hour,b_tim->tm_min);
}
字符串形式显示时间: 原型: char *asctime(const struct tm *tm)
功能:将struct tm格式的时间转换为字符串
函数参数:待转换的tm格式的时间 格林威治时间或本地时间
返回值:字符串显示的时间
例如:
#include <stdio.h>
#include <time.h>
int main(int argc,char *argv[])
{
time_t tim;
struct tm *g_tim;
struct tm *b_tim;
char *buf1=NULL;
char *buf2=NULL;
tim=time(NULL);
g_tim=gmtime(&tim); //注意函数参数类型
b_tim=localtime(&tim);
buf1=asctime(g_tim);
buf2=asctime(b_tim);
printf("%d %d %d %d %d \n",g_tim->tm_year+1900,g_tim->tm_mon+1,g_tim->tm_mday,g_tim->tm_hour,g_tim->tm_min);
printf("%d %d %d %d %d \n",b_tim->tm_year+1900,b_tim->tm_mon+1,b_tim->tm_mday,b_tim->tm_hour,b_tim->tm_min);
printf(“转换格林威治时间得到的字符串时间为:%s\n”,buf1);
printf(“转换本地时间得到的字符串时间为:%s\n”,buf2);
}
日历时间转换为本地时间: 原型:char *ctime(const time_t *timep)
功能:将日历时间转换为本地时间
头文件:<time.h>
参数:待转换的日历时间
返回值:返回字符串表示目前当地的本地时间
例如:
#include <stdio.h>
#include <time.h>
int main(int argc,char *argv[])
{
time_t tim;
struct tm *g_tim;
struct tm *b_tim;
char *buf1=NULL;
char *buf2=NULL;
char *c_buf=NULL;
tim=time(NULL);
g_tim=gmtime(&tim); //注意函数参数类型
b_tim=localtime(&tim);
buf1=asctime(g_tim);
buf2=asctime(b_tim);
c_buf=ctime(&tim);
printf("%d %d %d %d %d \n",g_tim->tm_year+1900,g_tim->tm_mon+1,g_tim->tm_mday,g_tim->tm_hour,g_tim->tm_min);
printf("%d %d %d %d %d \n",b_tim->tm_year+1900,b_tim->tm_mon+1,b_tim->tm_mday,b_tim->tm_hour,b_tim->tm_min);
printf(“转换格林威治时间得到的字符串时间为:%s\n”,buf1);
printf(“转换本地时间得到的字符串时间为:%s\n”,buf2);
printf(“日历时间转化为本地时间为:%s\n”,c_buf);
}
文件属性:
索引节点号 磁盘格式化或被分配时生成的
文件属性描述:
struct stat{
dev_t st_dev; //如果是设备,返回文件使用的设备号,否则为0
ino_t st_ino; //索引节点号
mode_t st_mode; //文件类型
nlink_t st_nlink; //文件的硬件连接数
uid_t st_uid; //所有者用户识别号
dev_t st_rdev; //设备类型
off_t st_size; //文件大小 字节表示
blksize_t st_blksize; //系统每次按块io操作时块的大小 一般为512或1024
blkcnt_t st_blocks; //块的索引号
time_t st_atime; //最后访问时间
time_t st_mtime; //最后修改时间
time_t st_ctime; //创建时间
};
获取文件属性:
stat和lstat的作用完全相同,都是取参数file_name所指的文件状态,其差别在于,当文件为符号链接时,
lstat()会返回该链接本身的状态,而不是非目标文件(也就是返回的是链接文件的信息)
stat 和fstat的差别 前者传递带路径的文件名或者目录名,后者传递的是文件的描述符。
stat 函数原型: int stat (const char *path,struct stat *buf)
第一个参数为传入的文件 第二个参数为属性 文件不打开执行stat
参数:path:文件路径 buf 返回文件的文件信息
功能:提供文件名字,获取文件对应属性
头文件:<unistd.h> <sys/stat.h> <sys/types.h>
返回值:成功返回0,失败返回-1
例如:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
int main(int argc,char argv[])
{
struct stat buf;
int ret=0;
ret=stat(argv[1],&buf);
if(ret0)
{
printf(“文件的索引节点号为:%ld\n”,buf.st_ino);
printf(“文件的类型为:%ld\n”,buf.st_mode);
}
else
{
perror(“stat”);
}
}
fstat : 函数原型: int fstat(int fds,struct stat *buf)
第一个参数为传入的文件描述符 第二个参数为属性 (文件打开后才能执行fstat)
函数功能:通过文件描述符获取文件对应的属性。
头文件:unistd.h sys/stat.h sys/types.h
参数:fds文件描述符 buf 返回文件的信息
返回值:成功返回0,失败返回-1
例如:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
struct stat buf;
int fd;
int ret=0;
fd=open(argv[1],O_RDWR); //以读写权限
ret=fstat(fd,&buf);
if(ret
0)
{
printf(“文件的索引节点号为:%ld\n”,buf.st_ino);
printf(“文件的类型为:%d\n”,buf.st_mode);
printf(“文件创建的时间为:%s\n”,ctime(&buf.st_ctime));
}
else
{
perror(“fstat”);
}
close(fd);
}
lstat : 函数原型:int lstat(const char
path,struct stat buf)
函数功能:链接文件描述符,获取文件属性,即可查看原文件,又可以看链接文件
头文件: unistd.h sys/types.h sys/stat.h
参数:path 文件路径 buf 返回文件的文件信息,针对符号链接,返回链接本身,而不是目标文件
例如:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
int main(int argc,char argv[])
{
struct stat buf;
int fd;
int ret=0;
fd=open(argv[1],O_RDWR); //以读写权限
ret=lstat(argv[1],&buf);
if(ret==0)
{
printf(“文件的索引节点号为:%ld\n”,buf.st_ino);
printf(“文件的类型为:%d\n”,buf.st_mode);
printf(“文件创建的时间为:%s\n”,ctime(&buf.st_ctime));
}
else
{
perror(“fstat”);
}
close(fd);
}
ls -ail 可以查看文件的详细信息:包括
索引节点 文件种类和权限 硬件连接数 拥有者 所归属的组 文件或目录的大小 最后访问或修改时间 文件名或目录名
文件种类:
[d] 目录
[-] 普通文件
[l] 链接文档(link file)
[b] 表示在装置文件里面的可供存储的接口设备(可随机存取装置) 块设备文件 二进制文件
[c] 表示为装置文件里的串行端口设备,例如键盘鼠标(一次性读取装置)
[-s] 套接字文件
[-p] 命名管道文件
硬件连接数 1就是它本身 大于1就是有快捷方式 ln -s 可以创建
判断文件类型:
S_ISLNK(mode) 判断是否是符号链接
S_ISREG(mode) 判断是否是普通文件
S_ISDIR(mode) 判断是否是目录
S_ISCHR(mode) 判断是否是字符型设备
S_ISBLK(mode) 判断是否是块设备
S_ISFIFO(mode) 判断是否是命名管道
S_ISSOCK(mode) 判断是否是套接字
使用示例: if(S_ISLNK(buf.st_mode)) printf(“该文件为链接文件”);
权限的核查:access
函数原型:int access(const char
pathname,int mode)
函数功能:可检测当前用户(运行这个程序的用户)对某文件是否有某权限
头文件:unistd.h
参数;pathname :文件或者是目录路径
mode R_OK 测试读权限
W_OK 测试写权限
X_OK 测试执行权限
F_OK 测试文件是否存在
返回值:若所有欲该查的文件权限都通过了检查测返回0,表示成功,只要有一项权限被禁止返回失败-1
注意:access如果是在普通用户下,只检测当前用的权限,如果是在root用户下,那末检测的是所有用户的权限
文件属性的应用:
可以获取文件的大小 获取文件创建时间 判断文件的属性 获取文件的权限
目录操作:
基本函数:
mkdir 函数原型: int mkdir(const char
pathname,mode_t mode)
函数功能:动态创建指定的目录
头文件: sys/stat.h sys/types.h
参数: pathname 文件路径
mode 给目录指定的权限 直接使用数字即可给权限
返回值:成功返回0,错误返回-1
例如:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
int main(int argc,char argv[])
{
int ret=0;
ret=mkdir(argv[1],0744);
if(ret==0)
{
printf(“创建目录成功\n”);
}
else
perror(“mkdir\n”);
}
rmdir : 函数原型: int rmdir(const char
pathname)
功能:删除目录
头文件:unistd.h
参数:要删除的目录路径
返回值:成功返回0,失败返回-1
例如:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
int ret=0;
ret=mkdir(argv[1],0744);
if(ret0)
{
printf(“创建目录成功\n”);
}
else
{
perror(“mkdir\n”);
}
ret=rmdir(argv[1]);
if(ret
0)
{ printf(“删除目录%s成功\n”,argv[1]); }
else { perror(“rmdir\n”);}
}
getcwd=PWD 函数原型: char getcwd(charbuf,size_t size)
函数功能:获取当前目录
头文件:unistd.h
参数:buf 保存当前目录缓存区 size:buf最大字节为255字节
返回值:成功返回指向当前目录的指针,和部分值一样,错误返回null
例如:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <strings.h>
#define MAX_ 255
int main(int argc,char argv[])
{
char buf[MAX_];
bzero(buf,sizeof(buf));
if(getcwd(buf,MAX_)==NULL)
{
perror(“getcwd\n”);
}
else
printf(“当前的目录路径为:%s\n”,buf);
}
cd chdir 函数原型: int chdir(const char
path)
函数功能:修改当前目录,切换目录,在代码里动态切换
头文件;unistd.h
参数:path 文件路径
返回值:成功返回0 失败返回-1
例如:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <strings.h>
#define MAX_ 255
int main(int argc,char *argv[])
{
char buf[MAX_];
bzero(buf,sizeof(buf));
if(getcwd(buf,MAX_)==NULL)
{
perror(“getcwd\n”);
}
else
printf(“当前的目录路径为:%s\n”,buf);

if(chdir("/home/wet/0")==0)
  {
  printf("路径切换成功\n");
  if(getcwd(buf,MAX_)==NULL)
    {
     perror("getcwd\n");
    }
   else 
   printf("当前的目录路径为:%s\n",buf);	  
  }

}
chmod 函数原型: int chmod(const char*path,mode_t mode)
函数功能:更改权限
头文件: sys/stat.h sys/types.h
参数:path 文件路径
mode 权限
返回值:成功返回0 失败返回-1
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <strings.h>
#define MAX_ 255
int main(int argc,char *argv[])
{

if(chmod("/home/wet/0",0777)==0)
{
printf(“给文件0权限成功\n”);
return 0;
}
else perror(“chmod\n”);
}

读取目录部分:
struct dirent结构体:存储目录中的文件信息(文件名 扩展名等)
#include <dirent.h>
struct dirent{
long d_ino; //索引节点号
off_t d_off; //在目录文件中的偏移
unsigned short d_reclen; //文件名长
unsigned char d_type; //文件类型
char d_name[NAME_MAX+1]; //文件名 最长255字符
}

opendir 函数原型:DIR *opendir(const char *name)
功能;打开目录
头文件:sys/types.h dirent.h
参数:目录的路径
返回值:成功返回指向当前目录的指针,错误返回null
closedir 函数原型:int closedir(DIR *dir)
函数功能:关闭目录
头文件: sys/types.h dirent.h
参数:opendir返回的指针
返回值:成功返回0 失败返回-1
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <dirent.h>
#include <strings.h>
#define MAX_ 255
int main(int argc,char *argv[])
{
DIR *dir;
dir=opendir(argv[1]);
if(dir!=NULL)
{
printf(“打开目录成功\n”);

}

else perror(“opendir”);
closedir(dir);
}
readdir 函数原型:struct dirent readdir(DIR dir)
功能:读取目录信息
头文件:sys/types.h dirent.h
参数:打开目录后返回的文件指针
返回值:成功返回目录的信息 错误返回null
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <dirent.h>
#include <strings.h>
#define MAX_ 255
int main(int argc,char argv[])
{
DIR dir;
struct dirent read_buf;
dir=opendir(argv[1]);
if(dir!=NULL)
{
printf(“打开目录成功\n”);
read_buf=readdir(dir);
while((read_buf=readdir(dir))!=NULL) //如果条件写(read_buf!=NULL)有问题,则read_buf固定了 要使read_buf随时获取到鼠标指针的状态
{
printf(“该目录下有%s\n”,read_buf->d_name);
}
}
else perror(“opendir”);
closedir(dir);
}
rewinddir 函数原型:void rewinddir(DIR dir)
功能:重新定位到目录文件的头部
头文件:<sys/types.h> <dirent.h>
参数:打开目录后返回的文件指针
返回值:无
seekdir 函数原型: void seekdir(DIR dir,off_t offset)
功能:设置参数dir目录流目前的读取位置,在调用readdir()时便从此新位置开始读取
参数offset代表距离目录文件开头的偏移量
头文件:sys/types.h dirent.h
参数:dir 打开目录后返回的文件指针
offset 代表距离目录文件开头的偏移量
返回值:无
注意:seekdir的偏移位置的变量是long int 类型
telldir 函数原型: off_t telldir(DIR dir)
函数功能:取得目录流的读取位置
头文件:sys/types.h dirent.h
参数:打开目录后返回的文件指针
返回值:成功返回距离目录文件开头的偏移量,返回值返回下个读取位置
有错误发生时返回-1
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <dirent.h>
#include <strings.h>
#define MAX_ 255
int main(int argc,char argv[])
{
long int i=0,offset;
DIR dir;
struct dirent read_buf;
dir=opendir(argv[1]);
if(dir!=NULL)
{
printf(“打开目录成功\n”);
read_buf=readdir(dir);
while((read_buf=readdir(dir))!=NULL) //如果条件写(read_buf!=NULL)有问题,则read_buf固定了 要使read_buf随时获取到鼠标指针的状态
{
if(++i==5)
offset=telldir(dir);
printf(“该目录下有%s\n”,read_buf->d_name);
}
printf("
*");
seekdir(dir,offset);
while((read_buf=readdir(dir))!=NULL)
{
printf(“该目录下有%s\n”,read_buf->d_name);
}
}
else perror(“opendir”);
closedir(dir);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值