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

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

在 C 语言中,ftell() 返回指定流相对于文件开头的当前文件位置。该函数用于将文件指针移动到文件末尾后获取文件的总大小。它以 long 类型返回当前位置,文件可以包含超过 32767 字节的数据。

这是 C 语言中 ftell() 的语法,

long int ftell(FILE *stream)登录后复制

  • stream - 这是指向 FILE 对象的指针,该对象标识

这是 C 语言中的 ftell() 示例。

假设我们有一个文件“one.txt”,其中包含以下内容。

This is demo text! This is demo text! This is demo text!登录后复制

示例

#include #include 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(); }登录后复制