文件操作总结

对于文件的基本操作函数很多,大致可以分为C标准库自带的带缓存的文件操作函数和LINUX下的底层不带缓存的文件操作函数。
1、打开文件

open():打开文件
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
fopen():打开文件
FILE *fopen(const char *path, const char *mode); 
FILE *fdopen(int fd, const char *mode);

对于open()函数,打开文件不论是读还是写,都不会清空清空文件的内容,除非以写的方式打开文件并且加了截断的操作,此时就会清空文件内的原有内容。
而对于fopen()函数开说,当以写的方式打开文件就会清空文件的内容,在(函数手册内详细说明了)。
以下说明摘自linux函数手册:
[w Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.]

FILE *fdopen(int fd, const char *mode);
int fileno(FILE *stream);

以上一组函数用于文件描述符和文件指针的转换。
2、读文件

ssize_t read(int fd, void *buf, size_t count);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

3、写文件

size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
ssize_t write(int fd, const void *buf, size_t count);

4、关闭文件

int close(int fd);
int fclose(FILE *fp);

5、定位文件内容

off_t lseek(int fd, off_t offset, int whence);
int fseek(FILE *stream, long offset, int whence);

对于以上的文件的操作函数,我们只需要记住他们的不同之处,和使用的细节,关于具体的函数参数和返回值,在linux手册中都有详细的说明[man 函数名]就会详细的说明,有的函数还带有例子说明。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值