Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ฟังก์ชัน ftell() ในภาษา C

ฟังก์ชัน ftell() คืนตำแหน่งปัจจุบันของไฟล์สตรีม (file position) นับจากจุดเริ่มต้นของไฟล์ ใช้หาขนาดไฟล์โดยย้ายตัวชี้ไฟล์ไปท้ายไฟล์ก่อน คืนค่าเป็น long int รองรับไฟล์ขนาดใหญ่กว่า 32,767 ไบต์

รูปแบบไวยากรณ์

long int ftell(FILE *stream)

พารามิเตอร์

  • stream - ตัวชี้ไปยังออบเจ็กต์ FILE ที่ระบุสตรีม

ตัวอย่าง

สมมติไฟล์ one.txt มีเนื้อหา:

This is demo text! This is demo text! This is demo text!
#include <stdio.h>
#include <conio.h>
void main() {
    FILE *f;
    int len;
    f = fopen("one.txt", "r");
    if (f == NULL) {
        perror("Error opening file");
        return -1;
    }
    fseek(f, 0, SEEK_END);
    len = ftell(f);
    fclose(f);
    printf("Size of file: %d bytes", len);
    getch();
}

ผลลัพธ์

Size of file: 78 bytes