Skip to content

文件操作

Tueo edited this page Mar 21, 2020 · 2 revisions

从fopen和open开始

fopen和open是两套不同的api接口,在linux下的fopen是open的封装函数,fopen最终还是要调用底层的系统调用open。fopen是c/c++标准I/O库函数。open是linux的系统调用。

fopen fclose fread fwrite freopen fseek ftell rewind
open close read write ioctl

fopen与open的区别

  1. fread是带缓冲的,read不带缓冲。
  2. fopen是标准c里定义的,open是POSIX中定义的。
  3. fread可以读一个结构,read在linux/unix中读二进制与普通文件没有区别。
  4. fopen不能指定要创建文件的权限,open可以指定权限。
  5. fopen返回文件指针,open返回文件描述符(整数)。
  6. linux/unix中任何设备都是文件,都可以用open,read。

函数功能及原型

open

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

pathname为文件路径,可以是相对路径,也可以是绝对路径。 flags是打开方式,如只读,只写或者读写。 mode指定文件权限

Clone this wiki locally