C 語言中的 ftell() 函式


在 C 語言中,ftell() 會返回相對於檔案起始位置的指定流的當前檔案位置。此函式用於在將檔案指標移動到檔案末尾後獲取檔案的總大小。它以 long 型別返回當前位置,而檔案可以包含超過 32767 個位元組的資料。

以下是 C 語言中 ftell() 的語法:

long int ftell(FILE *stream)

以下是 ftell() 中使用的引數:

  • stream − 指向標識流的 FILE 物件的指標。

以下是 C 語言中 ftell() 的一個示例。

假設我們有一個名為“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

更新於:2020-06-24

11,000+ 次瀏覽

開始您的 職業

完成課程並獲得認證

入門
廣告
© . All rights reserved.