15

I was just wondering why the member functions tellg() defined in basic_istream<> class and tellp() defined in basic_ostream<> class have different names. Is that because basic_fstream<> is derived from basic_istream<> and basic_ostream<> ?

Spook
  • 25,318
  • 18
  • 90
  • 167
John Kalane
  • 1,163
  • 8
  • 17

2 Answers2

8

tellg() get the position of the get pointer and tellp() gets the position of the put pointer, one of them is the place where you read and the second- where you write in the file. So the two functions do different things and return different values. Why would you think they should have the same name?

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • They both position the file pointer in two different classes – John Kalane Jan 25 '13 at 12:59
  • @user1577873 but you may open a file in an append mode so that both pointers are of an interest - you may both read and write to the file. – Ivaylo Strandjev Jan 25 '13 at 13:01
  • 1
    @IvayloStrandjev No, in a file they always both have the same value. They can be different in other kinds of streams, such as `std::stringstream`. – Potatoswatter Jan 25 '13 at 13:09
  • @Ivaylo That's exactly what I was referring to, when I said that the names are different probably because basic_fstream<> (for bidirectional files) is derived from the classes basic_istream<> and basic_ofstream<>. Otherwise the names could be the same. This way, it would be less confusing to the user. – John Kalane Jan 25 '13 at 13:10
  • @Potatoswatter are you sure even if you call `seekp` and `seekg`? – Ivaylo Strandjev Jan 25 '13 at 13:10
  • @IvayloStrandjev Yes, I (re-)wrote the buffering for GCC's standard library and the mechanism which keeps the get and put pointers in sync. – Potatoswatter Jan 25 '13 at 13:11
7

And how would you distinguish them in bidirectional streams, like std::fstream or std::stringstream? Streams are allowed to maintain separate pointers for input and output: fstream doesn't, but stringstream does. So you need either a flag to indicate which one you want (as is the case in streambuf), or you need two separate functions.

James Kanze
  • 150,581
  • 18
  • 184
  • 329