比思論壇
標題:
lseek操作
[打印本頁]
作者:
妖刀路过
時間:
前天 23:42
標題:
lseek操作
#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;
}
歡迎光臨 比思論壇 (http://bs111111.site/)
Powered by Discuz! X2.5