C++ 中的ftell() 函数返回文件指针的当前位置。
ftell()原型
long ftell(FILE* stream);
ftell()
函数将文件流作为其参数,并将给定流的文件位置指示符的当前值作为 long int 类型返回。
它在<cstdio> 头文件中定义。
参数:
stream
: 返回当前位置的文件流。
返回:
成功时,ftell()
函数返回文件位置指示符。否则,它返回 -1L。
示例:ftell() 函数的工作原理
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int pos;
char c;
FILE *fp;
fp = fopen("file.txt", "r");
if (fp)
{
while ((c = getc(fp)) != EOF)
{
pos = ftell(fp);
cout << "At position " << pos << ", character is " << c << endl;
}
}
else
{
perror("Error reading file");
}
fclose(fp);
return 0;
}
运行程序时,输出将是:
At position 1, character is P At position 2, character is r At position 3, character is o At position 4, character is g At position 5, character is r At position 6, character is a At position 7, character is m At position 8, character is i At position 9, character is z At position 10, character is . At position 11, character is c At position 12, character is o At position 13, character is m
相关用法
- C++ fcvt()用法及代码示例
- C++ fwscanf()用法及代码示例
- C++ fmax()用法及代码示例
- C++ fdim()用法及代码示例
- C++ fmin()用法及代码示例
- C++ fetestexcept()用法及代码示例
- C++ forward_list::unique()用法及代码示例
- C++ forward_list::emplace_front()用法及代码示例
- C++ fopen()用法及代码示例
- C++ forward_list::max_size()用法及代码示例
- C++ forward_list::reverse()用法及代码示例
- C++ feupdateenv()用法及代码示例
- C++ forward_list::swap()用法及代码示例
- C++ forward_list::front()、forward_list::empty()用法及代码示例
- C++ functional::bad_function_call用法及代码示例
- C++ find_if()用法及代码示例
- C++ find()用法及代码示例
- C++ forward_list::remove()用法及代码示例
- C++ fgetws()用法及代码示例
- C++ forward_list::operator=用法及代码示例
注:本文由纯净天空筛选整理自 C++ ftell()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。