目录

long int ftell(FILE *stream)

描述 (Description)

C库函数long int ftell(FILE *stream)返回给定流的当前文件位置。

声明 (Declaration)

以下是ftell()函数的声明。

long int ftell(FILE *stream)

参数 (Parameters)

  • stream - 这是指向标识流的FILE对象的指针。

返回值 (Return Value)

此函数返回位置指示器的当前值。 如果发生错误,则返回-1L,并将全局变量errno设置为正值。

例子 (Example)

以下示例显示了ftell()函数的用法。

#include <stdio.h>
int main () {
   FILE *fp;
   int len;
   fp = fopen("file.txt", "r");
   if( fp == NULL )  {
      perror ("Error opening file");
      return(-1);
   }
   fseek(fp, 0, SEEK_END);
   len = ftell(fp);
   fclose(fp);
   printf("Total size of file.txt = %d bytes\n", len);
   return(0);
}

我们假设我们有一个文本文件file.txt ,它具有以下内容 -

This is iowiki.com

现在让我们编译并运行上面的程序,如果文件有上述内容,将产生以下结果,否则它将根据文件内容给出不同的结果 -

Total size of file.txt = 26 bytes
↑回到顶部↑
WIKI教程 @2018