Jump to content
  • Advertisement
Sign in to follow this  

streambuf overloading

This topic is 4126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Couple days back I made a post, and got refered to the idea of overloading a streambuf to provide some extra functionality in my file access, while maintaining the c++ look of fstreams. That said, I figured out how to overload it so that
  my_stream << "test" << endl;
will call "overflow" for each of t-e-s-t-NULL. That is nice, so I'm partway there. now when i try to do
  my_stream >> a_string;
it always pulls up an empty string, but the odd part is that it never calls "underflow" so Im wondering what i have to overload, or what code i have to add to "overflow" inorder to get it to return something; After that, Im assuming that "sync" is where i need to update my pointers and such. So, is sync called on a seekp/seekg? if so then how do i find the position so i can update my underlying file?
class vfs_file_buff : public streambuf
		void	open		( string fname );
		void	close		( void );

		int		underflow	( void );		
		int		overflow	( int ch );
		//int		sync		( void );


void vfs_file_buff::open	( string fname )
	cout << "open" << endl;

void vfs_file_buff::close ( void )
	cout << "close" << endl;

int vfs_file_buff::underflow ( void )
	cout << "underflow" << endl;
	return 0;

int vfs_file_buff::overflow ( int ch )
	cout << "overflow" << endl;
	cout << (char)ch << endl;
	return ch;

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
sync is used when you call flush on a stream, and is optional to implement.

The 3 functions I implemented are overflow (for reading), and underflow + uflow for writing. After profiling my (very) simple game though, I think the one-char at a time method has too much overhead, and I will need to implement a buffered solution.

Share this post

Link to post
Share on other sites
Anyone have a good link/code for that? cauze i just added uflow to my overloads, but it seems that it is never getting there either.

Ok, it appears to have something to do with my initialization.
If I seed a "ostream" then the overflow works.
If I seed a "istream" then the underflow works.
If I seed a "iostream" then the overflow works, but not the underflow.

--edit again
Ok, so the issue had everything to do with me attempting to do a seekg/p on the stream.
Wich leads be back to my last question. How do i get the streambuf position so i can sync that with
my underlying buffers/files?

[Edited by - KulSeran on March 4, 2007 9:03:06 PM]

Share this post

Link to post
Share on other sites
For anyone interested:


appear to be the functions that have to be overwritten inorder to properly overload a streambuf
for input and output


also have to be overwritten inorder to allow the above stream to be seeked.

When i finish my code will likely post if for those intersted.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!