主页 > 创业  > 

文件IO(20250217)

文件IO(20250217)
1. 文件IO

系统调用Linux内核提供的文件操作接口

1. 打开文件  open 2. 读写文件  read/write 3. 关闭文件  close

1.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:创建,创建时可添加0664赋予被创建文件权限O_TRUNC:清空存在的文件O_APPENDD:文件存在时,(文件末尾)追加写

与标准IO的打开方式对比:

‘r’:O_RDONLY'r+':O_RDWR'w':O_RDONLY | O_CREAT | O_TRUNC'w+':O_RDWR | O_CREAT | O_TRUNC'a':O_WRONLY | O_CREAT | O_APPEND'a+':RDWR | O_CREAT | O_APPEND

返回值:             成功返回文件描述符 (最近最小未使用)             失败返回-1

1.2 文件描述符

小且非负整型数据,代表了一个已打开的文件

操作系统会为已打开文件分配文件描述符默认文件描述符范围:0-1023;文件描述符分配原则:最小未被使用原则

系统已打开的三个文件描述符:0, 1, 2

0:标准输入设备,对于标准io的stdin1:标准输出设备,对应标准io的stdout;2:标准出错设备,对应标准io的stderr 

已打开的文件在使用完毕后,应及时关闭文件,否则会导致文件描述符泄露。当文件描述符不够用时,可通过修改文件配置提升文件描述符上限。

r/w/x分别对应可读、可写和可执行

1.3 write函数

ssize_t write(int fd,  const  void *buf, size_t count);         功能:             通过文件描述符向文件中写一串数据         参数:             fd:文件描述符             buf:要写入文件的字符串的首地址             count:要写入字符的个数         返回值:              成功返回实际写入的个数             失败返回-1

1.4 read函数

        ssize_t read(int fd, void *buf, size_t count);         功能:             通过文件描述符读取文件中的数据         参数:             fd:文件描述符             buf:存放数据空间的首地址             count:要读到数据的个数         返回值:             成功返回读到数据的个数             失败返回-1             读到文件结尾返回0

1.5 文件偏移函数lseek

off_t lseek(int fd, off_t offset, int whence);         功能:             重定位一个文件描述符的偏移量(文件定位器)         参数:             fd:文件描述符             offset:偏移量                         正:向后偏移                         负:向前偏移                         零:不偏移             whence:                 SEEK_SET                 SEEK_CUR                 SEEK_END         返回值:             成功返回当前偏移量的值(off_t   long int)             失败返回-1 

2. 时间相关函数

头文件time.h

2.1 time 

t        ime_t time(time_t *t);           功能:             获得1970年1月1日到现在的秒数            参数:             t:存放秒数空间的首地址           返回值:             成功返回1970年1月1日到现在的秒数 (time_t   long)             失败返回-1

2.2 ctime

        char *ctime(const time_t *timep);           功能:             将秒数转换为字符串时间           参数:             timep:1970年1月1日到现在的秒数           返回值:             成功返回时间字符串首地址             失败返回NULL

2.3 localtime

        struct tm *localtime(const time_t *timep);            功能:             将秒数转换为时间结构体            参数:             timep:存放秒数空间首地址            返回值:             成功返回包含时间结构体空间首地址             失败返回NULL

struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ }; 3. printf函数

*int printf(const char *format, ...);

        将格式化的字符串打印到终端

int fprintf(FILE *stream, const char *format, ...);

        将格式化后的字符串写入到文件流指针所对用的文件

int dprintf(int fd, const char *format, ...);

        将格式化后的字符串写入到文件描述符所对用的文件里

*int sprintf(char *str, const char *format, ...);

        将格式化后的字符串写入到str所指向的内存空间 

标签:

文件IO(20250217)由讯客互联创业栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“文件IO(20250217)