Notice
Recent Posts
Recent Comments
Today
Total
05-19 00:04
Archives
관리 메뉴

Jeongchul Kim

리눅스 lseek - 리눅스 시스템 프로그래밍 본문

Linux

리눅스 lseek - 리눅스 시스템 프로그래밍

김 정출 2016. 2. 29. 11:15


리눅스 lseek - 리눅스 시스템 프로그래밍


lseek는 지정한 파일에 대해 읽기/쓰기 포인터의 위치를 변경이 가능하다.

파일을 오픈하고 읽기/쓰기 포인터를 파일의 맨 마지막 부분으로 이동하게 될 경우, 포인터의 위치를 임의로
이동할 수 있다.


off_t lseek(int filedes, off_t offset, int whence);


> filedescriptor : 읽기/쓰기 포인터를 수행할 파일에 대한 파일 기술자 이다.

> offset : 새롭게 지정할 읽기/쓰기 포인터의 위치를 의미합니다. 기준에 따라 음수가 될 수도 있습니다.

> whence : offset의 기준입니다.

- SEEK_SET : 파일의 맨 처음

- SEEK_CUR : 현재 포인터의 위치

- SEEK_END : 파일의 맨 마지막(EOF)를 시작점으로 한다.

> return 값 : 작업이 성공하면 파일의 첫 부분을 기준으로 한 포인터의 오프셋을 반환합니다.

작업이 실패할 경우 -1이 반환됩니다.



헤더파일 : <sys/types.h> <unistd.h>


#include <unistd.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <stdio.h>

#include <string.h>

int main()

{

int fd;

off_t pointer;


fd = open("lseek.txt", O_RDWR | O_CREAT, 0644);

pointer = lseek(fd, (off_t)0, SEEK_CUR);

printf("first postion : %d\n", pointer);

write(fd,"lseek",strlen("lseek"));


fd = open("lseek.txt", O_RDWR | O_APPEND);

printf("last postion : %d\n", pointer);

write(fd,"lseek",strlen("lseek"));


close(fd);

}






Comments