69,381
社区成员
发帖
与我相关
我的任务
分享
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fd;
off_t length;
char *addr;
char *inserted = "awdawdasdadw啊我大王的哇啊伟大我的哇awdawdawdasdawdadafdaw124234\n"; // this str will be inserted to the file
fd = open("abc.txt", O_RDWR | O_CREAT, 0644);
length = lseek(fd,0,SEEK_END);
addr = (char *)mmap(NULL,4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
for (int i = 0;i < 30;i++)
{
memcpy(addr + length, inserted, strlen(inserted));
length += strlen(inserted) + 1;
}
//printf("addr: %s", addr);
munmap((void *)addr, length);
close(fd);
}