ฟังก์ชัน fseek() ใช้ย้ายตัวชี้ไฟล์ (file pointer) ไปตำแหน่งที่กำหนด คืนค่า 0 หากสำเร็จ คืนค่าที่ไม่ใช่ 0 หากล้มเหลว
รูปแบบไวยากรณ์
int fseek(FILE *stream, long int offset, int whence)
พารามิเตอร์
- stream - ตัวชี้ระบุสตรีม
- offset - จำนวนไบต์จากตำแหน่งอ้างอิง
- whence - ตำแหน่งอ้างอิง
ค่าคงที่สำหรับ whence
SEEK_END- ท้ายไฟล์SEEK_SET- เริ่มต้นไฟล์SEEK_CUR- ตำแหน่งปัจจุบันของตัวชี้ไฟล์
ตัวอย่าง
ไฟล์ demo.txt มีเนื้อหา:
This is demo text! This is demo text! This is demo text! This is demo text!
#include <stdio.h>
void main() {
FILE *f;
f = fopen("demo.txt", "r");
if (f == NULL) {
printf("\n Can't open file or file doesn't exist.");
exit(0);
}
fseek(f, 0, SEEK_END);
printf("The size of file : %ld bytes", ftell(f));
getch();
}ผลลัพธ์
The size of file : 78 bytes