複製鏈接
請複製以下鏈接發送給好友

pwrite

鎖定
寫入起始地址的偏移量,寫入地址=文件開始+offset。注意,執行後,文件偏移指針不變
中文名
pwrite
公    式
寫入地址=文件開始+offset
結    果
執行後,文件偏移指針不變
要    求
寫入起始地址的偏移量

pwrite函數名

pwrite

pwrite功能

偏移量地寫數據到文件中

pwrite函數原型

ssize_t pwrite(intfd, const void *buf, size_tcount, off_toffset);

pwrite用法

返回值:成功,返回寫入到文件中的字節數;失敗,返回-1;
參數:
(1) fd:要寫入數據的文件描述符
(2) buf:數據緩存指針,存放要寫入文件中的數據
(3) count:寫入文件中的數據的字節數
(4) offset:偏移地址

pwrite程序實例

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void main()
{
int fd;
int count = 128;
int offset = 32;
int ret;
char buf[1024]="hi ! this is pwrite.";
char pathname[128] = "/tmp/1.txt";
fd = open( pathname, O_WRONLY);
if((ret = pwrite(fd, buf, count, offset))==-1)
{
printf("pwrite error\n");
exit(1);
}
else
{
printf("pwrite success\n");
printf("the writed data is:%s\n", buf);
}
}