Dotcpp  >  编程教程  >  io.h头文件  >  C语言tell()函数:获取打开文件的指针位置

C语言tell()函数:获取打开文件的指针位置

点击打开在线编译器,边学边练

函数名:tell

头文件:<io.h>

函数原型: int tell(int handle);

功能:获取打开文件的指针位置

参数:int handle  为要获取文件指针的文件句柄

返回值: 成功   返回给定文件的文件指针的位置 ,失败   返回-1


程序例:创建文件,内容为I like www.dotcpp.com very much!

//打开文件,移动七个字节,获取当前指针位置

#include<stdio.h>

#include<io.h>

#include<fcntl.h>

int main(void){

   int fd=open("D:\\a.txt",O_RDONLY);

   if(fd==-1){

      printf("can not open the file\n");

      return 1;

   }

   int pos=tell(fd);

   printf("before lseek function,current position: %ld\n",pos);

   lseek(fd,7,SEEK_SET);  //移动到以文件的开头偏移7个字节的位置

   pos=tell(fd);

   printf("after lseek function,current position: %ld\n",pos);

   close(fd);

   return 0;

}

 

运行结果

before lseek function,current position: 0
after lseek function,current position: 7



本文固定URL:https://www.dotcpp.com/course/462

C语言函数库
assert.h头文件
ctype.h头文件
float.h头文件
io.h头文件
math.h头文件
mem.h头文件
setjmp.h头文件
stdio.h头文件
stdlib.h头文件
sigal.h头文件
string.h头文件
time.h头文件
Dotcpp在线编译      (登录可减少运行等待时间)