要将文件指针移动到文件末尾,可以使用 fseek 函数并设置偏移量为 0,起始位置为文件末尾的位置。以下是一个示例代码:
#include <iostream> #include <fstream> int main() { std::ifstream file("example.txt", std::ios::binary); if (!file.is_open()) { std::cerr << "Failed to open file." << std::endl; return 1; } // 将文件指针移动到文件末尾 file.seekg(0, std::ios::end); // 获取当前文件指针位置(即文件大小) std::streampos fileSize = file.tellg(); std::cout << "File size: " << fileSize << " bytes." << std::endl; file.close(); return 0; } 这段代码打开一个名为 “example.txt” 的文件,并使用 seekg 函数将文件指针移动到文件末尾。然后通过 tellg 函数获取文件指针位置(即文件大小),最后关闭文件。