Need to use streams/buffers, but NOT related to cout, cin or to files

R

Ramon F Herrera

I have looked all over the net for this, but all the hits I get are
about cout, cin or streams linked to disk files.

The feature that I need is in-core (boy, that's an ancient term)
streams and buffers.

Back when I was a C programmer, I implemented my own streaming like
this:

char output_buffer[150 *1024];
char pointer = *output_buffer;

pointer += sprintf(pointer, "Some text here %s\n", somevariable1);
pointer += sprintf(pointer, "Some more text here %s\n",
somevariable2);
pointer += sprintf(pointer, "Some further stuff here %s\n",
somevariable3);

I then discovered something that looks exactly what I need, but it is
inside the Boost library. See snippet below.

I bet that standard C++ and/or the STL must contain some facility that
performs the functionality of the code below.

TIA,

-Ramon

--------------------------

boost::asio::streambuf request;
std::eek:stream request_stream(&request);
request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
request_stream << "Host: " << argv[1] << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

// Send the request.
boost::asio::write(socket, request);
 
P

peter koch

I have looked all over the net for this, but all the hits I get are
about cout, cin or streams linked to disk files.

The feature that I need is in-core (boy, that's an ancient term)
streams and buffers.

Back when I was a C programmer, I implemented my own streaming like
this:

char output_buffer[150 *1024];
char pointer = *output_buffer;

pointer += sprintf(pointer, "Some text here %s\n", somevariable1);
pointer += sprintf(pointer, "Some more text here %s\n",
somevariable2);
pointer += sprintf(pointer, "Some further stuff here %s\n",
somevariable3);

Go read a book - that can be really helpfull. What you need is
std::stringstream or its related brethern.

/Peter
 
R

Ramon F Herrera

I have looked all over the net for this, but all the hits I get are
about cout, cin or streams linked to disk files.
The feature that I need is in-core (boy, that's an ancient term)
streams and buffers.
Back when I was a C programmer, I implemented my own streaming like
this:
char output_buffer[150 *1024];
char pointer = *output_buffer;
pointer += sprintf(pointer, "Some text here %s\n", somevariable1);
pointer += sprintf(pointer, "Some more text here %s\n",
somevariable2);
pointer += sprintf(pointer, "Some further stuff here %s\n",
somevariable3);

> Go read a book - that can be really helpful.
> What you need is std::stringstream or its related brethern.

/Peter

Books are great, but a combination of Peter Koch's guidance and a book
is even better.

I am in a remote place in Venezuela, paper books are out of the
question. My books are in Boston.

Thanks,

-Ramon
 
A

Alf P. Steinbach

* Ramon F Herrera:
I have looked all over the net for this, but all the hits I get are
about cout, cin or streams linked to disk files.

The feature that I need is in-core (boy, that's an ancient term)
streams and buffers.

Back when I was a C programmer, I implemented my own streaming like
this:

char output_buffer[150 *1024];
char pointer = *output_buffer;

pointer += sprintf(pointer, "Some text here %s\n", somevariable1);
pointer += sprintf(pointer, "Some more text here %s\n",
somevariable2);
pointer += sprintf(pointer, "Some further stuff here %s\n",
somevariable3);

I then discovered something that looks exactly what I need, but it is
inside the Boost library. See snippet below.

I bet that standard C++ and/or the STL must contain some facility that
performs the functionality of the code below.

TIA,

-Ramon

--------------------------

boost::asio::streambuf request;
std::eek:stream request_stream(&request);
request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
request_stream << "Host: " << argv[1] << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

// Send the request.
boost::asio::write(socket, request);

You're already using Boost.

Boost has some support for "in-core" streams, that is, memory mapped streams,
for interprocess communication.

But possibly what you want is something far simpler, namely like

#include <sstream>
#include <string>

...
std::eek:stringstream stream;
stream << "Some text here " << someVariable1 << "\n";
stream << "Some more text here " << someVariable2 << "\n";
stream << "Some further stuff here " << someVariable3 << "\n";

std::string const s = stream.str();

where std::eek:stringstream is one of the std::stringstream "bethren"[?] that Peter
Koch Larsen referred you to.


Cheers & hth.,

- Alf
 
R

Ramon F Herrera

* Ramon F Herrera:


I have looked all over the net for this, but all the hits I get are
about cout, cin or streams linked to disk files.
The feature that I need is in-core (boy, that's an ancient term)
streams and buffers.
Back when I was a C programmer, I implemented my own streaming like
this:
char output_buffer[150 *1024];
char pointer = *output_buffer;
pointer += sprintf(pointer, "Some text here %s\n", somevariable1);
pointer += sprintf(pointer, "Some more text here %s\n",
somevariable2);
pointer += sprintf(pointer, "Some further stuff here %s\n",
somevariable3);
I then discovered something that looks exactly what I need, but it is
inside the Boost library. See snippet below.
I bet that standard C++ and/or the STL must contain some facility that
performs the functionality of the code below.
--------------------------

    boost::asio::streambuf request;
    std::eek:stream request_stream(&request);
    request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
    request_stream << "Host: " << argv[1] << "\r\n";
    request_stream << "Accept: */*\r\n";
    request_stream << "Connection: close\r\n\r\n";
    // Send the request.
    boost::asio::write(socket, request);

You're already using Boost.

Boost has some support for "in-core" streams, that is, memory mapped streams,
for interprocess communication.

But possibly what you want is something far simpler, namely like

     #include <sstream>
     #include <string>

     ...
     std::eek:stringstream stream;
     stream << "Some text here " << someVariable1 << "\n";
     stream << "Some more text here " << someVariable2 << "\n";
     stream << "Some further stuff here " << someVariable3 << "\n";

     std::string const s = stream.str();

where std::eek:stringstream is one of the std::stringstream "bethren"[?] that Peter
Koch Larsen referred you to.

Cheers & hth.,

- Alf

Thanks so much, Alf!

I intend to use both kinds of streams: the simpler string-based for
day to day programming, and the fancier memory-mapped from Boost for
networking and advanced applications.

Let me tell you what turned me off about the Boost streams. I found
out how to insert data into the "stream black box" ('request' variable
in the snippet) but I can't figure out how to access it.

I found that there is a 'request.data' field, but I cannot convert it
to a string. The most I have achieved is to print out the size of the
back box:

cout << request.size() << " elements" << endl;

-Ramon

ps: Has anybody been able to use the Boost Gmane newsgroup?
 
P

peter koch

[snip]
where std::eek:stringstream is one of the std::stringstream "bethren"[?] that Peter
Koch Larsen referred you to.

Just me that missed an "r"; it should be "brethren". Looking it up, I
found out the word was not that accurate and that it is also archaic.
Oh well...

/Peter
 
P

peter koch

I have looked all over the net for this, but all the hits I get are
about cout, cin or streams linked to disk files.
The feature that I need is in-core (boy, that's an ancient term)
streams and buffers.
Back when I was a C programmer, I implemented my own streaming like
this:
char output_buffer[150 *1024];
char pointer = *output_buffer;
pointer += sprintf(pointer, "Some text here %s\n", somevariable1);
pointer += sprintf(pointer, "Some more text here %s\n",
somevariable2);
pointer += sprintf(pointer, "Some further stuff here %s\n",
somevariable3);

 > Go read a book - that can be really helpful.
 > What you need is std::stringstream or its related brethern.



Books are great, but a combination of Peter Koch's guidance and a book
is even better.

I am in a remote place in Venezuela, paper books are out of the
question. My books are in Boston.

If you have an internet connection, there should be plenty of
opportunities to seek information about e.g. the standard library. If
I can't remember some specific detail, I often just google it: good
stuff is out there, but of course you have to find it.

In fact, the first hit when googling "C++ stream" was to a site which
contained information about stringstreams. So now that you've learned
to google, being away from Boston is no longer an excuse. ;-)

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top