LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2005, 04:41 AM   #1
Ossar
Member
 
Registered: Oct 2004
Distribution: Debian, Ubuntu
Posts: 38

Rep: Reputation: 15
[C++] buffers streams


Lets say I have a "char * buffer". My problem is how do I tie an ostream to a streambuf so that I can write the buffer of characters to this streambuf.

buffer -> ostream -> streambuf

The streambuf is used by other objects to fetch data for further processing.

I have tried something like this:
Code:
streambuf * sb;
ostream os(sb);
char * buffer = "Hello";

os.write(buffer, 5);
os.flush();
cout << sb;
Not working!

Last edited by Ossar; 06-14-2005 at 04:51 AM.
 
Old 06-14-2005, 12:27 PM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
First off, the value in the pointer sb is undefined. On some compilers this will be 0 or some other predetermined value, on others it will be whatever happened to be in the memory before.

You should never use a pointer before setting it's value; this includes passing it to a constructor or function.

None of the standard << operator methods take a pointer to an object as far as I recall (although they will accept a pointer to a NULL-terminated string in a char array).

From the standard, 27.5 Para 1:
Quote:
The header <streambuf> defines types that control input from and output to character sequences
streambuf is a specialization of basic_streambuf<class charT, class traits> to work with char streams. It takes an input stream, buffers it, and writes it to an output stream when the buffer gets full. You haven't set an output stream in the above code, so it's not clear what your code should do if the pointer were valid.

basic_streambuf also has only a protected constructor, meaning that it can only be created as a base class. I guess this is probably why you're trying to use a pointer here. This won't work; you really do need an object of type streambuf — and that means creating a subclass object like filebuf or stringbuf.

It seems to me like what you want to be using here is an stringbuf, which gives stream access to a character string.

As an aside, I recommend using the std::string class over a NULL-terminated char array; it makes it much easier to trap errors when pointers get out of bounds or buffers overflow, and the overhead is really very small compared to the power of most modern PCs. (Some embedded PCs may be a special case).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP Buffers awksquawk Linux - Networking 1 11-18-2005 04:41 AM
saving vi buffers frostillicus Linux - Newbie 5 04-19-2005 09:03 PM
mplayerplugin buffers to 99% then nothing happens vavoem Linux - Software 9 10-13-2004 01:03 AM
About buffers? eshwar_ind Programming 2 04-30-2004 05:13 AM
eth1: no RX buffers subhasis_ray Linux - Networking 1 11-11-2002 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration