首页 > 文章列表 > 在C语言中,ftell()函数用于获取文件指针的当前位置

在C语言中,ftell()函数用于获取文件指针的当前位置

c语言 文件指针位置 ftell()函数
176 2023-08-27

在 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