系统调用open () ,read(),write(),lseek();
库函数fopen(), fread(),fwrite(),flseek();
库函数
1.fopen()函数
函数功能 |
打开文件 |
头文件 |
#include<stdio.h> |
函数原型 |
FILE *fopen(const char *path ,const char *mode) |
参数说明 |
§path:欲打开的文件路径与文件名 §mode:打开方式: “r”:以只读方式打开文件 “r+”:以可写可读方式打开文件 “w”:以只写方式打开文件 “w+”:以可读可写的方式打开文件,若文件不存在则建立文件 “a”:以追加的方式打开文件,若文件不存在则建立文件; 若文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。 “a+”:相对“a”功能的基础上,以追加方式打开可读可写的文件。 |
返回值 |
文件打开成功,返回文件指针; 文件打开失败,则返回NULL,并把错误码存在errno中 |
范例: #include<stdio.h> Main() { FILE *fp; fp=fopen(“hell.txt”,”a+”); if(fp==NULL) return: fclose(fp); } |
1. open()函数
所需头文件 |
#include <sys/types.h> #include <sys/stat.h > #include <fcntl.h>
|
|
函数原型 |
(1)Int open(const char *pathname, int flags); (2)Int open(const char *pathname, int flags, mode_t mode ); |
|
区别当flag使用了O_CREAT标志,则使用第二种形式,这时需要mode来指定访问权限
|