快速掌握Linux(2)(时间相关函数)(文件IO)

5.时间相关函数

1)time()

#include <time.h>

time_t time(time_t *tloc);

功能:获取1970-1-1 00:00:00到现在的秒数

参数:

tloc:保存秒数的变量的地址

返回值:

获得的秒数

2)ctime()

char *ctime(const time_t *timep);

功能:将秒数转换成字符串时间

参数:

timep :秒数的变量的地址

返回值:

字符串时间

3)localtiome()

struct tm *localtime(const time_t *timep);

功能:将秒数转换成日历时间

struct tm {

int tm_sec; /* Seconds (0-60) */

int tm_min; /* Minutes (0-59) */

int tm_hour; /* Hours (0-23) */

int tm_mday; /* Day of the month (1-31) */

int tm_mon; /* Month (0-11) */

int tm_year; /* Year - 1900 */

int tm_wday; /* Day of the week (0-6, Sunday = 0) */

int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */

int tm_isdst; /* Daylight saving time */

};

6. 文件IO

文件IO: Linux内核提供的文件操作接口。属于系统调用。

学习接口:

1.打开文件 : open()

2.读文件、写文件 : read() 、write()

3.关闭文件 : close()

函数接口:

1)open

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int open(const char *pathname, int flags);

int open(const char *pathname, int flags, mode_t mode);

功能:打开一个文件并获得一个文件描述符

参数:

pathname:要打开的文件的文件名

flags:打开方式

O_RDONLY :只读

O_WRONLY :只写

O_RDWR :读写

O_CREAT :创建

O_TRUNC : 清空

O_APPEND : 追加

"r" O_RDONLY

"r+" O_RDWR

"w" O_WRONLY | O_TRUNC | O_CREAT, 0664

"w+" O_RDWR | O_TRUNC | O_CREAT,0664

"a" O_WRONLY | O_CREAT | O_APPEND, 0664

"a+" O_RDWR | O_CREAT | O_APPEND, 0664

mode:用户自己、同组用户、其他用户对该文件的读写执行权限

(mode & ~umask)

普通文件:0664

权限给满:0777

rwxrwxr-x

返回值:

成功:文件描述符

失败:-1

文件描述符:

当打开一个文件时,系统会为已打开的文件分配一个文件描述符用来标记该文件。

文件描述符:小的,非负的整形数据

操作系统默认可分配出的文件描述符:0-1023: 总共1024个

文件描述符的分配原则:最小未被使用

系统默认已打开的文件:

标准IO: 文件IO

FILE * int

stdin 0(STDIN_FILENO)

stdout 1(STDOUT_FILENO)

stderr 2 (STDERR_FILENO)

2)close

#include <unistd.h>

int close(int fd);

功能:关闭文件

参数:

fd:文件描述符

返回值:

成功:0

失败:-1

注意:

只打开文件,使用完不关闭文件会造成文件描述符泄露。

3)write

ssize_t write(int fd, const void *buf, size_t count);

功能:向文件中写入数据

参数:

fd :要写入的文件描述符

buf:要写入的数据首地址

count:希望写入的字节数

返回值:

成功:实际写入的字节数

失败:-1

4)read

ssize_t read(int fd, void *buf, size_t count);

功能:从文件中读取数据

参数:

fd:要读的文件的文件描述符

buf:存储读取数据的空间首地址

count:希望读到的字节数

返回值:

成功:返回实际读到的字节数

失败:-1

到达文件文件末尾:0

文件拷贝:

int file_copy(const char *srcfile, const char *dstfile)

{

int fdsrc = open(srcfile, O_RDONLY);

int fddst = open(dstfile, O_WRONLY | O_CREAT | O_TRUNC, 0664);

if (-1 == fdsrc || -1 == fddst)

{

printf("open error\n");

return -1;

}

char buff1024 = {0};

ssize_t size = 0;

while ((size = read(fdsrc, buff, sizeof(buff))) > 0)

{

write(fddst, buff, size);

}

close(fdsrc);

close(fddst);

return 0;

}

5)文件定位函数:

(1)lseek()

off_t lseek(int fd, off_t offset, int whence);

功能:重新定位文件的读写位置

参数:

fd:文件描述符

offset:偏移量

whence:要偏移的起始位置

SEEK_SET :从文件开头偏移

SEEK_CUR: 从当前读写位置偏移

SEEK_END :从文件末尾偏移

返回值:

成功:返回当前读写位置到文件开头的偏移量

失败:-1

7.文件IO和标准IO的区别

缓冲区

(1)行缓冲

默认大小1024字节(1K),主要使用在人机交互终端上。

刷新条件:

    1. 遇到'\n'刷新
    2. 程序结束刷新
    3. 使用fflush()强制刷新
    4. 缓冲区满时自动刷新
(2)全缓冲

默认大小4096字节(4K),文件交互

刷新条件:

  1. 程序结束刷新
  2. 文件关闭自动刷新
  3. 全缓冲区满时自动刷新
  4. 使用fflush()强制刷新
(3)无缓冲

0k, ,出错信息输出,标准出错设备

stderr

相关推荐
郭涤生1 小时前
VScode回归ACM模式(古法编程)配置
linux·ide·vscode
苏子寒1 小时前
Nano-VLLM全代码解析笔记(2)-block_manager
人工智能·笔记·python·机器学习·nlp·vllm
Forever Nore1 小时前
LeetCode 16 最接近的三数之和 - 双指针逼近
算法·leetcode·职场和发展
盐焗鹌鹑蛋1 小时前
【Linux】ELF与库加载
java·linux·服务器
小猴子爱上树1 小时前
跨境电商图片翻译工具,跨马批量处理视频字幕与抠图
python·音视频
smj2302_796826521 小时前
解决leetcode第4033题有效K个不同元素子数组I
数据结构·python·算法·leetcode
Patrick在香港1 小时前
Python asyncio vs 多线程 vs 多进程——同一份 HKOpenDataClient 实测 5/30/100 endpoint,谁是真的银弹
开发语言·python
Metaphor6921 小时前
使用 Python 设置 Excel 文件属性
开发语言·python·excel
梦诺1 小时前
LINUX定时执行脚本
linux·运维·服务器