parc65
parc65

Reputation: 191

tellg moves after write operation in file

Why does tellg() moves after a write operation, I suppose it should be 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();

Output:

5

Upvotes: 0

Views: 331

Answers (1)

user2100815
user2100815

Reputation:

The stream actually maintains only one pointer, so the read and write pointers are effectively the same. if you want to do reads and writes to the same file, you should maintain your own pointer and do a seek before every read/write operation.

Upvotes: 2

Related Questions