0

I'm practicing file handling. I made a text file and wrote some characters in it, now I want the cursor to go to the beginning of the file and put a character there using seekp.

Here is the code

fstream ofile;

char ch = 'g';

ofile.open("test.txt", ios::out | ios::app);

ofile.seekp(0, ios::beg);
ofile.put(ch);

ofile.close();

Expected Result: It writes g at the start of file.

Actual Result: It appends g at the end.

I tried removing the ios::app tag but ios::out alone truncates the file before writing anything.

Idaisa
  • 19
  • 10

1 Answers1

-1

See: http://en.cppreference.com/w/cpp/io/basic_fstream/open

std::ios::app seek to the end of stream before each write

Gábor Buella
  • 1,840
  • 14
  • 22