[- Disclaimer -]
아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
fseek()
✦ <stdio.h>
✦ Argument3 위치로 Argument2만큼 Stream 포인터 이동
✦ 파일 Open 시 a Mode는 return FALSE 되지 않지만 무시 됨
✧ Ex) abc가 있으면 a 앞에 써야 할지, c 뒤에 써야 할지 판단 불가
✦ 파일 열기 시 Update Mode에서 다음 Update Mode 시 Stream 조작 필요
✧ Read/Write 했던 걸 다음 Write/Read 시 썼던 Stream 포인터를 사용하면 안 되기 때문
→ a+는 앞에를 못 바꾸고 뒤에만 바꿀 수 있어 뒤로 계속 Write만 가능
✦ Parameter1에 전달되는 Argument1
✧ 파일 Stream 포인터 자체 주소
✦ Parameter2에 전달되는 Argument2
✧ Stream 포인터 offset
✦ Parameter3에 전달되는 Argument3
✧ 매크로 상수
SEEK_SET: 맨 처음 위치 기준
SEEK_CUR: 현재 위치 기준
SEEK_END: 마지막 위치 기준
Plain Text
복사
✦ Return 값
✧ 성공
→ 0
✧ 실패
→ 0 이외의 값
test.txt에 "ABC"가 적혀 있을 경우 - Ex)
#include <stdio.h>
int main(void) {
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (fp != NULL) {
int c = 0;
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
fseek(fp, -1L, SEEK_CUR); // Stream 포인터를 현재 위치 기준으로 한칸 이전으로 이동
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
}
fclose(fp);
fp = NULL;
}
Plain Text
복사
ABAB
--------------------------------
Process exited after 0.4793 seconds with return value 0
계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
#include <stdio.h>
int main(void) {
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (fp != NULL) {
int c = 0;
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
fseek(fp, 1L, SEEK_CUR); // Stream 포인터를 현재 위치 기준으로 한 칸 이후로 이동
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
}
fclose(fp);
fp = NULL;
}
Plain Text
복사
AB
--------------------------------
Process exited after 0.7317 seconds with return value 0
계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
#include <stdio.h>
int main(void) {
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (fp != NULL) {
int c = 0;
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
fseek(fp, 0L, SEEK_SET); // rewind와 같은 효과가 되버렸음
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
}
fclose(fp);
fp = NULL;
}
Plain Text
복사
ABAB
--------------------------------
Process exited after 0.445 seconds with return value 0
계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사
#include <stdio.h>
int main(void) {
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (fp != NULL) {
int c = 0;
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
fseek(fp, 0L, SEEK_END); // EOF만 2번 Stream에 읽어오는 꼴이 됨
c = getc(fp);
printf("%c", c);
c = getc(fp);
printf("%c", c);
}
fclose(fp);
fp = NULL;
}
Plain Text
복사
AB
--------------------------------
Process exited after 0.3723 seconds with return value 0
계속하려면 아무 키나 누르십시오 . . .
Plain Text
복사



