tellg在文件中执行写入操作后移动

tellg moves after write operation in file

本文关键字:操作 移动 执行 文件 tellg      更新时间:2023-10-16

为什么tellg()在写操作之后移动,我想它应该是tellp()?

std::fstream fs("c:\log.txt", std::ios::in | std::ios::out | std::ios::trunc);
fs << "write";
std::cout << fs.tellg() << std::endl;
fs.close();

输出:

5

流实际上只维护一个指针,因此读指针和写指针实际上是相同的。如果要对同一个文件进行读写操作,则应维护自己的指针,并在每次读写操作之前进行查找。