#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd, bytenum;
int currpos;
char wbuf[10] = "123456789";
char rbuf[10];
if ((fd = open("./a.text1", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IXUSR)) < 0) {
perror("open failed!");
return -1;
}
if ((bytenum = write(fd, wbuf, sizeof(wbuf))) < 0) {
perror("write failed!");
return -1;
}
if ((currpos = lseek(fd, 4, SEEK_SET)) < 0){
perror("lseek failed!");
return -1;
}
if ((bytenum = read(fd, rbuf, sizeof(rbuf))) < 0) {
perror("read failed!");
return -1;
}
bytenum = sizeof(wbuf);
printf("wbuf size:%d\n",bytenum);
printf("read from rbuf:%s\n",rbuf);
close(fd);
return 0;
}
C
最新推荐文章于 2024-09-21 11:32:39 发布