seekg用于移动文件读取指针,tellg获取当前指针位置,二者结合可实现文件的随机访问。示例中先用tellg记录初始位置,读取一行后再次调用tellg获取新位置,随后用seekg跳回文件开头重新读取,再跳至文件末尾获取文件大小,最后跳转到指定偏移读取部分内容。处理大文件或二进制数据时需以binary模式打开文件,避免文本模式换行符转换导致的定位错误。常见陷阱包括越界访问和未检查流状态,正确使用可高效实现文件索引、跳转和解析。

C++中,
seekg
tellg
ifstream
seekg
tellg
在使用C++处理文件时,特别是当你需要非顺序地读取文件内容,或者需要知道文件大小、当前读取进度时,
seekg
tellg
ifstream
seekg()
istream& seekg (streampos pos);
pos
streampos
long long
istream& seekg (streamoff off, ios_base::seekdir dir);
dir
off
off
streamoff
dir
ios_base::seekdir
ios_base::beg
ios_base::cur
ios_base::end
tellg()
streampos
立即学习“C++免费学习笔记(深入)”;
基本用法示例:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::ofstream ofs("example.txt");
ofs << "Hello World!" << std::endl;
ofs << "C++ File IO is cool." << std::endl;
ofs.close();
std::ifstream ifs("example.txt");
if (!ifs.is_open()) {
std::cerr << "无法打开文件!" << std::endl;
return 1;
}
// 1. 获取当前位置
std::streampos initialPos = ifs.tellg();
std::cout << "初始读取位置: " << initialPos << std::endl; // 通常是0
// 2. 读取一行
std::string line;
std::getline(ifs, line);
std::cout << "读取第一行: " << line << std::endl;
// 3. 再次获取当前位置
std::streampos afterFirstLinePos = ifs.tellg();
std::cout << "读取第一行后的位置: " << afterFirstLinePos << std::endl; // 会是"Hello World!\n"的字节长度
// 4. 使用 seekg 移动到文件开头
ifs.seekg(0, std::ios::beg); // 等价于 ifs.seekg(initialPos);
std::streampos afterSeekBeg = ifs.tellg();
std::cout << "移动到文件开头后的位置: " << afterSeekBeg << std::endl;
// 5. 再次读取第一行,验证是否成功移动
std::getline(ifs, line);
std::cout << "再次读取第一行: " << line << std::endl;
// 6. 移动到文件末尾并获取文件大小
ifs.seekg(0, std::ios::end);
std::streampos fileSize = ifs.tellg();
std::cout << "文件大小: " << fileSize << " 字节" << std::endl;
// 7. 移动到文件中间某个位置
// 假设我们想从 "C++ File IO is cool." 的 "File" 开始读
// "Hello World!\n" 大约13个字节,加上 "C++ " 4个字节,再加一个空格,就是18个字节
ifs.seekg(13 + 4 + 1, std::ios::beg); // 从开头偏移18个字节
std::cout << "移动到文件中间某个位置后的位置: " << ifs.tellg() << std::endl;
std::getline(ifs, line); // 这会从当前位置读到行尾
std::cout << "从中间读取: " << line << std::endl;
ifs.close();
// 清理文件
std::remove("example.txt");
return 0;
}在我看来,文件读取位置的精确控制,是文件操作从“线性阅读”到“随机访问”的关键转变。试想一下,如果你有一本厚厚的字典,你不会从第一页开始一页一页地翻找一个词,而是会直接跳到对应的字母部分。文件操作也是如此。
很多时候,我们处理的文件并非简单文本,它们可能是:
没有
seekg
tellg
seekg
seekg
首先,再强调一下它的两个核心参数:
offset
dir
offset
streamoff
dir
std::ios::beg
offset
seekg(100, std::ios::beg)
std::ios::cur
offset
seekg(-50, std::ios::cur)
std::ios::end
offset
seekg(-20, std::ios::end)
常见陷阱:
文本模式与二进制模式的差异:这是最容易犯错的地方!如果你以文本模式(默认模式)打开文件,
seekg
\n
\r\n
tellg
seekg
std::ios::binary
std::ifstream ifs("data.bin", std::ios::binary);越界操作:尝试将指针移动到文件开头之前(负偏移量超出文件开头)或文件末尾之后,可能会导致流进入错误状态(
failbit
badbit
seekg
if (!ifs.good()) { /* 处理错误 */ }大文件处理:虽然
streamoff
streamoff
streamoff
streampos
SetFilePointerEx
lseek64
tellg
tellg
streampos
streamoff
正确理解和规避这些陷阱,能让你在使用
seekg
tellg
seekg
tellg
seekg
获取文件大小:这是最常见的用法之一。通过将读取指针移动到文件末尾,然后用
tellg
std::ifstream ifs("my_large_file.bin", std::ios::binary);
if (!ifs.is_open()) { /* 错误处理 */ }
ifs.seekg(0, std::ios::end); // 移动到文件末尾
std::streampos fileSize = ifs.tellg(); // 获取当前位置,即文件大小
ifs.seekg(0, std::ios::beg); // 移回文件开头,准备读取
std::cout << "文件大小: " << fileSize << " 字节" << std::endl;在我看来,这种方法比读取整个文件来计算大小要高效得多,特别是对于大文件。
“标记”与“返回”机制:在解析复杂文件格式时,你可能需要先读取一部分数据,然后根据这些数据决定跳到文件的另一个区域继续读取,最后再返回之前的位置。
tellg
seekg
// 假设你正在解析一个自定义格式文件
std::ifstream ifs("complex_data.dat", std::ios::binary);
// ... 读取一些头部信息 ...
// 假设头部信息告诉你,真正的数据块在文件某个特定偏移量处
// 但你可能需要先处理一个中间的索引区
std::streampos bookmark = ifs.tellg(); // 记住当前位置
// 跳到索引区处理
ifs.seekg(index_offset, std::ios::beg);
// ... 读取并处理索引数据 ...
// 处理完索引后,跳回之前标记的位置继续读取主数据
ifs.seekg(bookmark);
// ... 继续读取主数据 ...这种模式在处理嵌套结构或交叉引用数据的文件时特别有用。
相对偏移与精确跳转:当你需要从当前位置精确地向前或向后跳跃特定字节数时,
seekg
std::ios::cur
// 假设你当前在文件中的某个位置 // 想跳过接下来的N个字节 long long bytesToSkip = 1024; ifs.seekg(bytesToSkip, std::ios::cur); // 从当前位置向前跳1024字节
或者,你也可以先
tellg
seekg(current_pos + offset)
std::streampos currentPos = ifs.tellg(); long long relativeOffset = 50; // 相对当前位置向前50字节 ifs.seekg(currentPos + relativeOffset);
我个人更倾向于后一种方式,因为它在逻辑上更清晰,特别是当涉及到复杂的偏移计算时。
streampos
streamoff
理解这些高级用法,能够让你在处理文件时拥有更强大的控制力,构建出更健壮、更高效的文件处理程序。它们是C++文件I/O的基石,值得花时间深入掌握。
以上就是C++文件位置控制 seekg tellg函数用法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号