本文主要是介绍linux,lseek,append用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
打开写的.c文件
内容为
代码
<sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>//off_t lseek(int fd, off_t offset, int whence);
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);
//ssize_t write(int fd, const void *buf, size_t count);
//ssize_t read(int fd, void *buf, size_t count);
#define filename "mm"
#define writeNum 128
#define readNum 12
int main()
{int offset;int fd;char writeBuff[writeNum] = {0};char readBuff[readNum] = {0};char *test = "hello world";if(writeNum<(strlen(test)+1)){printf("error:writeBuff less than test\n");return -1;}strcpy(writeBuff,test);fd = open("mm",O_APPEND|O_RDWR|O_CREAT,0755);if(fd == -1){printf("open failed!");perror("why");return -1;}printf("open successed!");// offset = lseek(fd,1,SEEK_CUR);printf("offset is %d\n",offset);write(fd,&writeBuff[0],11);
// read(fd,&readBuff,1);
// printf("%s \n",readBuff);close(fd);return 0;}
每编译一次,cat一次,会在末尾append一次 hello world
这篇关于linux,lseek,append用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!