I have noticed this strange thing
fstream obj(filename , ios::in);
obj.seekp(7);
is the same as
fstream obj(filename , ios::in);
obj.seekg(7);
seekg and seekp do the same this and cause the same outcome, although i specified ios::in flag only
Why are they both work with fstream? And what is the difference between seekp and seekg with fstream?
basic_fstreamis derived frombasic_iostreamwhich is derived frombasic_istreamandbasic_ostream. So,basic_fstreamhas functionseekpfrombasic_ostreamand functionseekgfrombasic_ifstream.In short, in your case, calls to seekp and seekg do same actions, since actions perfomed by
basic_filebuf::seekposdepends only on open mode forbasic_filebuf.Where
pubseekposcallsseekpos(which isvirtual, so, callsbasic_filebuf::seekpos)Since you open file with
ios_base::infunction does 2 and 3 puncts.