吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C 中的 ftell() 示例

C 中的 ftell() 示例

来源:网络 | 更新时间:2022-01-26 07:57:04
C 中的 ftell() 用于找出文件中文件指针相对于文件开头的位置。ftell() 的语法是:
long ftell(FILE *pointer)
考虑下面的 C 程序。示例中的文件包含以下数据: “Someone over there is calling you. We are going for work. Take care of yourself.” (不带引号) 执行 fscanf 语句时,单词“Someone”存储在字符串中,并且指针移动到“Someone”之外。因此 ftell(fp) 返回 7,因为“Someone”的长度为 6。
// 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 包含“Someone over there ....”。
7

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com