- 金錢
- 3
- 威望
- 587
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 0 小時
- 最後登錄
- 2025-11-16
- 主題
- 77
- 精華
- 0
- 閱讀權限
- 30
- 註冊時間
- 2011-11-18
- 帖子
- 273
 
TA的每日心情 | 無聊 昨天 10:22 |
|---|
簽到天數: 235 天 [LV.7]常住居民III - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 3
- 威望
- 587
- 主題
- 77
|
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define FILE1_NAME "lseek_test.c"
#define FILE2_NAME "lseek_test2.txt"
#define SIZE 100
int main(void) {
int file1, file2;
char buffer[1024];
int ret;
file1 = open(FILE1_NAME, O_RDONLY);
if (file1 < 0) {
printf("open file failed! reason:%s\n", strerror(errno));
exit(-1);
}
file2 = open(FILE2_NAME, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (file2 < 0) {
printf("open file failed! reason:%s\n", strerror(errno));
exit(-2);
}
//文件句柄,偏移量,从哪偏移
ret = lseek(file1, 0, SEEK_END);
printf("file size: %d\n", ret);
ret = lseek(file1, 100, SEEK_SET);
printf("lseek ret:%d\n", ret);
ret = read(file1, buffer, SIZE);
if (ret > 0) {
buffer[ret] = '\0';
write(file2,buffer,SIZE);
}
close(file1);
close(file2);
return 0;
}
|
|