Hàm ftell() trả về vị trí hiện tại của con trỏ file (file position) so với đầu file. Dùng để lấy kích thước file sau khi đưa con trỏ về cuối file. Trả về kiểu long int (hỗ trợ file > 32767 byte).
Cú pháp
long int ftell(FILE *stream);
Tham số
- stream — con trỏ
FILExác định luồng.
Ví dụ
Giả sử file one.txt có nội dung:
This is demo text! This is demo text! This is demo text!
#include <stdio.h>
#include <conio.h>
void main() {
FILE *f;
long len;
f = fopen("one.txt", "r");
if (f == NULL) {
perror("Loi mo file");
return;
}
fseek(f, 0, SEEK_END); // Di chuyển đến cuối file
len = ftell(f); // Lấy vị trí = kích thước
fclose(f);
printf("Kich thuoc file: %ld bytes", len);
getch();
}
Kết quả
Kich thuoc file: 78 bytes
Lưu ý: Trên hệ thống 64-bit, nên dùng ftello() (trả về off_t) cho file lớn > 2GB. Luôn kiểm tra fopen thành công trước khi dùng.