user7336243
user7336243

Reputation:

How to find the file pointer position in c++?

I am currently working with files in c++ and I want to read a file after a certain position. I read online that you can't open a file to read and write simultaneously. Is there a way to return the position of the file pointer at a certain moment and use it to extract the information after it?

Upvotes: 0

Views: 5695

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69902

What you read was wrong.

#include <fstream>
#include <string>

int main()
{
  std::fstream file("test.txt", std::ios::in | std::ios::out | std::ios::binary);

  std::string s;

  file >> s;

  // get current read position
  auto read_pos = file.tellg();

  // set current write position
  file.seekp(read_pos, std::ios::beg);
  static const char data[] = "aaa";

  // write some data
  file.write(data, 3);
}

Upvotes: 1

Related Questions