std:: ftell
From cppreference.net
C++
Input/output library
| I/O manipulators | ||||
| Print functions (C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(C++20)
|
||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
| Synchronized Output | ||||
|
(C++20)
|
||||
| Types | ||||
| Error category interface | ||||
|
(C++11)
|
||||
|
(C++11)
|
C-style I/O
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cstdio>
|
||
|
long
ftell
(
std::
FILE
*
stream
)
;
|
||
返回文件流
stream
的文件位置指示器当前值。
如果流以二进制模式打开,此函数获取的值是从文件起始位置开始的字节数。
如果流以文本模式打开,此函数返回的值是未指定的,仅作为 std::fseek 的输入时才有意义。
目录 |
参数
| stream | - | 要检查的文件流 |
返回值
成功时返回文件位置指示符,若发生失败则返回 - 1L 。失败时同时设置 errno 。
注释
在 Windows 系统上,可使用
_ftelli64
来处理大于 2 GiB 的文件。
示例
演示带错误检查的
std::ftell()
用法。将若干浮点数值写入文件后从文件读取。
运行此代码
#include <cstdio> #include <cstdlib> #include <iostream> // If the condition is not met then exit the program with error message. void check(bool condition, const char* func, int line) { if (condition) return; std::perror(func); std::cerr << func << " failed in file " << __FILE__ << " at line # " << line - 1 << '\n'; std::exit(EXIT_FAILURE); } int main() { // Prepare an array of FP values. constexpr int SIZE {5}; double A[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5}; // Write array to a file. const char* fname = "/tmp/test.bin"; FILE* file = std::fopen(fname, "wb"); check(file != NULL, "fopen()", __LINE__); const int write_count = std::fwrite(A, sizeof(double), SIZE, file); check(write_count == SIZE, "fwrite()", __LINE__); std::fclose(file); // Read the FP values into array B. double B[SIZE]; file = std::fopen(fname, "rb"); check(file != NULL, "fopen()", __LINE__); long pos = std::ftell(file); // position indicator at start of file check(pos != -1L, "ftell()", __LINE__); std::cout << "pos: " << pos << '\n'; const int read_count = std::fread(B, sizeof(double), 1, file); // read one FP value check(read_count == 1, "fread()", __LINE__); pos = std::ftell(file); // position indicator after reading one FP value check(pos != -1L, "ftell()", __LINE__); std::cout << "pos: " << pos << '\n'; std::cout << "B[0]: " << B[0] << '\n'; // print one FP value return EXIT_SUCCESS; }
可能的输出:
pos: 0 pos: 8 B[0]: 1.1
参见
|
获取文件位置指示器
(函数) |
|
|
将文件位置指示器移动到文件中的特定位置
(函数) |
|
|
将文件位置指示器移动到文件中的特定位置
(函数) |
|
|
返回输入位置指示器
(
std::basic_istream<CharT,Traits>
的公开成员函数)
|
|
|
返回输出位置指示器
(
std::basic_ostream<CharT,Traits>
的公开成员函数)
|
|
|
C 文档
for
ftell
|
|