#include <unistd.h> ssize_t read(int filedes, void* buff, size_t nbytes);成功则返回实际读取的byte数,如果已经达到文件结尾则返回0,出错则返回-1.
#include <unistd.h> ssize_t write(int filedes, void* buff, size_t nbytes);成功则返回实际写入的byte数,出错则返回-1.
每个打开的文件都有一个关联的“当前偏移量”,用于记录从文件到当前当前位置的偏移字节数,lseek函数是设置这个当前偏移量
#include <unistd.h> off_t lseek(int filedes, off_t offset, int whence);成功则返回新的文件偏移量,失败则返回-1.
off_t currpos; currpos = lseek(fd, 0, SEEK_CUR);
#include <fcntl.h> #include <stdio.h> int main(void){ int fd,byteNum,result; char wbuf[10] = "123456789"; char rbuf[10]; if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){ perror("open"); return -1; } if((byteNum = write(fd, wbuf, 10))<0){ perror("write"); return -1; } if((result = lseek(fd, 4, SEEK_SET))<0){ perror("lseek"); return -1; } if((byteNum = read(fd, rbuf, 10)) < 0){ perror("read"); return -1; } printf("read content:%s\n",rbuf); close(fd); return 0; }运行结果:
#include <fcntl.h> #include <stdio.h> int main(void){ int fd,byteNum,result; char wbuf[10] = "123456789"; char rbuf[10]; if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){ perror("open"); return -1; } if((byteNum = write(fd, wbuf, 10))<0){ perror("write"); return -1; } if((result = lseek(fd, -1, SEEK_SET))<0){ perror("lseek"); return -1; } if((byteNum = read(fd, rbuf, 10)) < 0){ perror("read"); return -1; } printf("read content:%s\n",rbuf); close(fd); return 0; }运行结果:
#include <fcntl.h> #include <stdio.h> int main(void){ int fd,byteNum,result; char wbuf[10] = "123456789"; char rbuf[10]; if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IXUSR))<0){ perror("open"); return -1; } if((byteNum = write(fd, wbuf, 10))<0){ perror("write"); return -1; } if((result = lseek(fd, -4, SEEK_CUR))<0){ perror("lseek"); return -1; } if((byteNum = read(fd, rbuf, 10)) < 0){ perror("read"); return -1; } printf("read content:%s\n",rbuf); close(fd); return 0; }运行结果:
#include <fcntl.h> #include <stdio.h> int main(void){ int fd,byteNum,result; char wbuf[10] = "123456789"; char rbuf[10]; if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR))<0){ perror("open"); return -1; } if((byteNum = write(fd, wbuf, 10))<0){ perror("write"); return -1; } if((result = lseek(fd, 40960, SEEK_END))<0){ perror("lseek"); return -1; } if((byteNum = write(fd, wbuf, 10)) < 0){ perror("write"); return -1; } close(fd); return 0; }运行结果:
#include <stdio.h> #include <fcntl.h> int main(void){ int fd,result; char wbuf[5] = "1234"; if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){ perror("open"); return -1; } if((result = lseek(fd, 2, SEEK_SET)) < 0){ perror("lseek"); return -1; } if((result = write(fd, wbuf, 4))<0){ perror("write"); return -1; } close(fd); return 0; }程序执行前a.txt为:123456789
#include <stdio.h> #include <fcntl.h> int main(void){ int fd,result; char wbuf[5] = "1234"; if((fd=open("a.txt",O_WRONLY|O_APPEND))<0){ perror("open"); return -1; } if((result = lseek(fd, -2, SEEK_END)) < 0){ perror("lseek"); return -1; } if((result = write(fd, wbuf, 4))<0){ perror("write"); return -1; } close(fd); return 0; }运行结果: