I was experimenting with file pointers in C++. In below code, the result obtained is 0, 30, 10 and 12. So it means that tellp() does not give correct result in append mode if we do seekp(). I was expecting tellp() to give me 32 after seekp() and appending data. I understand that in app mode, writing is always to the end and hence got this doubt. Does the result mean that tellp() position does not matter in append mode?
The contents of h1.txt file is : 01234567891011121314151617181911 and is is as expected.
ofstream f1("h1.txt",ios::app|ios::out);
if (!f1)
{
cerr<<"cannot open the file\n";
exit(-1);
}
currentPos = f1.tellp();
cout<<"Initial Write Pointer Position = "<<currentPos<<endl;
// Write some data at the end
for(int index=0;index<20;index++)
{
f1<<index;
}
currentPos= f1.tellp();
cout<<"Write Pointer Position after write = "<<currentPos<<endl;
f1.seekp(10);
currentPos= f1.tellp();
cout<<"Write Pointer Position after seek = "<<currentPos<<endl;
/** Note: Even if you reposition pointer, in app mode, data will always be written to the end */
f1<<11;
currentPos= f1.tellp();
/** seekp does not match. In app mode, data is writtten to end */
cout<<"Final Write Pointer Position after seek and write = "<<currentPos<<endl;
f1.close();
Visual C++ 2015 compiled app prints 0, 30, 10, 32.
Could be a bug in the version of C++ standard library you have. What compiler are your using?
来源:https://stackoverflow.com/questions/48393195/whether-tellp-not-defined-in-append-mode