리눅스 질문
글쓴이: 익명 사용자 / 작성시간: 일, 2021/04/11 - 1:05오전
fdopen과 lseek를 활용해서 fseek를 구현하려 합니다.
임의로 파일을 생성하고 제가 구현한 프로그램을 돌리고 난 후 ls -l로 파일 사이즈를 확인하는 방식으로 확인하려 했습니다.
파일을 생성하고 Linux라고 입력을 했고 사이즈가 6byte가 나온걸 확인했고
이후에 제 프로그램을 실행시켜 30byte만큼 fseek 하게 하려고 했고 그럼 36byte가 나와야하는데 여전히 6byte가 나오더군요 뭐가 문제일까요
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
FILE *fp;
int fd;
int offset;
printf("Enter Jumping Bytes : ");
scanf("%d",&offset);
if(argc != 3)
{
perror("Parameter Error!\n");
exit(-1);
}
else
{
if((fd = open(argv[1], O_RDONLY, 0644)) == -1)
{
perror("File Open Error!\n");
exit(-1);
}
if((fp = fdopen(fd, "r")) == NULL)
{
perror("Stream Creating Error!\n");
exit(1);
}
if(strcmp(argv[2], "SEEK_SET") == 0)
{
if(lseek(fd, offset, SEEK_SET) == -1)
{
perror("lseek Error!\n");
exit(1);
}
}
else if(strcmp(argv[2], "SEEK_CUR") == 0)
{
if(lseek(fd, offset, SEEK_CUR) == -1)
{
perror("lseek Error!\n");
exit(1);
}
}
else if(strcmp(argv[2], "SEEK_END") == 0)
{
if(lseek(fd, offset, SEEK_END) == -1)
{
perror("lseek Error!\n");
exit(1);
}
}
}
fclose(fp);
return 0;
}Forums:

https://man7.org/linux/man
https://man7.org/linux/man-pages/man2/lseek.2.html
lseek() allows the file offset to be set beyond the end of the file (but this does not change the size of the file). If data is later written at this point, subsequent reads of the data in the gap (a "hole") return null bytes ('\0') until data is actually written into the gap.lseek() 매뉴얼 페이지에 나오길, 파일 끝 너머로 lseek() 할 수는 있으나 그 위치에 쓰기가 이뤄지기 전까지는 lseek()만으로 파일크기가 바뀌지는 않는다는군요. 그리고, 지금은 파일을 읽기전용으로 열고 있기도 하네요.
댓글 달기