Board index » cppbuilder » fstream mystic?

fstream mystic?


2008-04-05 12:10:15 AM
cppbuilder111
Hi,
I'm opening a file in mode: ios::out|ios::in|ios::binary;
I want to move the put pointer and get pointer to different locations but they seem to be "tied" together?
The code below shows that a call to seekp not only moves the put pointer but also the get pointer.
Is that expected behaviour? I can't find anything about that in the specs?
int main(int argc, char* argv[])
{
string aFile("d:\\data_2\\Channel_0_m\\SAMPLE0.DAT");
fstream theFile;
theFile.open(aFile.c_str(), ios::out|ios::in|ios::binary);
if(!theFile)
{
return 0;
}
int pos;
cout<<"Put pointer position :"<<theFile.tellp()<<endl;
cout<<"Get pointer position :"<<theFile.tellg()<<endl;
long sectors = 187;
cout<<"moving put pointer\n";
theFile.seekp(sectors * 512, ios_base::cur);
cout<<"Put pointer position :"<<theFile.tellp()<<endl;
cout<<"Get pointer position :"<<theFile.tellg()<<endl;
return 0;
}
output:
Put pointer position :0
Get pointer position :0
moving put pointer
Put pointer position :95744
Get pointer position :95744
??
 
 

Re:fstream mystic?

"Totte Karlsson" < XXXX@XXXXX.COM >wrote in message
Quote
I want to move the put pointer and get pointer to different
locations but they seem to be "tied" together?
A file does not have separate get and put pointers. It only has a single
get/put pointer. This is handled all the way at the OS level, not the RTL
level.
Quote
The code below shows that a call to seekp not only moves
the put pointer but also the get pointer. Is that expected behaviour?
Yes. I do not know why fstream has separate methods for manipulating the
same pointer. I imagine that tellg() does different processing than tellp()
does. There is also a seekg() method as well.
Gambit
 

Re:fstream mystic?

Well, that explains it!
However, just reading documentation about fstreams really don't make sense then.
I've been treating file positioning in a file, wether opened in read/write mode, differently.
Using seekg on a write only file, and seekp on a read only file don't make much sense to me.
However, I tried that and it "works".
Thanks
-totte
Remy Lebeau (TeamB) wrote:
Quote
A file does not have separate get and put pointers. It only has a single
get/put pointer. This is handled all the way at the OS level, not the RTL
level.



Gambit


 

{smallsort}

Re:fstream mystic?

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >writes:
Quote

>The code below shows that a call to seekp not only moves
>the put pointer but also the get pointer. Is that expected behaviour?

Yes. I do not know why fstream has separate methods for
manipulating the same pointer. I imagine that tellg() does
different processing than tellp() does. There is also a seekg()
method as well.
These operations belong to the stream interfaces (*g() to the istream
and *p() to the ostream interface (and yes, I'm aware that they are
templates)). The stream interfaces are abstractions; they allow us to
uniformly access files, strings, the keyboard, the console, and other
backends for which user-provided code provides implementations
(e.g. sockets).
Since fstream provides both input and output, it makes sense for it to
implement both istream and ostream, even if the respective behaviors
aren't independent.
An example of a stream class with independent get and put pointers is
std::stringstream.