int open(const char* pathname, int flags);//文件名,打开方式
int open(const char* pathname, int flags, mode_t mode);//文件名,打开方式 权限
int create() //不常用
int close(int fd);
与printf和scanf有所区别,read和write是将文件读至缓冲区或从缓冲区写入文件,是否会偏移呢? a.读写过程中文件描述词fd不会改变,始终指向文件。 b. read 过程中虽然是相同的文件描述词,但是会存在一个偏移,不会读取已经读取过的文件内容。也就是说对于文件中存放“HelloWorld”,调用两次相同的read,第一次会读进缓冲区“Hello”,第二次读进缓冲区"World" 。缓冲区中内容会被覆盖。 c. write过程类似,文件中会产生偏移,而对于缓冲区不会,两次调用write,缓冲区中存放“HelloWorld”,文件中会是"HelloHello"。 d.write和read共用一个文件中的偏移。
改变文件大小
#include<unistd.h>
//将fd指定文件的大小,改为参数length指定大小。
int ftruncate(int fd, off_t length);
fd必须表示的是打开的可写入的文件描述词。
如果原来文件比length大,则超过部分会被删除。
返回值 成功返回0, 失败返回-1 。
文件定位
#include<sys/types.h>
#include<unistd.h>
off_t lseek(int fd, off_t offset, int whence);