當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 ftell()用法及代碼示例

C中的ftell()用於查找文件指針相對於文件開始在文件中的位置。 ftell()的語法是:

long ftell(FILE *pointer)

考慮下麵的C程序。該示例中獲取的文件包含以下數據:
“那邊有人調用你。我們去上班。好好照顧自己。” (無引號)
當執行fscanf語句時,單詞“Someone”被存儲在字符串中,並且指針移到“Someone”之外。因此,由於“someone”的長度為6,因此ftell(fp)返回7。

// C program to demonstrate use of ftel()                                 
#include<stdio.h> 
  
int main() 
{ 
    /* Opening file in read mode */
    FILE *fp = fopen("test.txt","r"); 
  
    /* Reading first string */
    char string[20];    
    fscanf(fp,"%s",string); 
  
    /* Printing position of file pointer */
    printf("%ld", ftell(fp)); 
    return 0; 
}

輸出:假設test.txt包含“某人……”。


7


相關用法


注:本文由純淨天空篩選整理自 ftell() in C with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。