473,785 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string looses contents.

I am downloading over http port 80 some contents froma site, but the
contents is not properly stored after using ostringstream for temporary
storage, and later ostringstream:: str() for passing it over to a string.
This doesn't work. It misses data and teh result is not the same as when
using char buffer[bufsize], which gives correct download. Could anyone
point out why the ostringstream + string way doesn't work, while the char
buffer[buffsize] does?
tia
--
http://www.kolumbus.fi/bob.smith
Jul 23 '05 #1
8 1539
* Bob Smith:
I am downloading over http port 80 some contents froma site, but the
contents is not properly stored after using ostringstream for temporary
storage, and later ostringstream:: str() for passing it over to a string.
This doesn't work. It misses data and teh result is not the same as when
using char buffer[bufsize], which gives correct download. Could anyone
point out why the ostringstream + string way doesn't work, while the char
buffer[buffsize] does?


It can be related to a bug-ridden download code that has exquisite timing
requirements.

However, it's most likely only related to incorrect usage of
'ostringstream' .

Since I suspect that your code intermingles the aspects of platform-specific
downloading and C++ buffering I won't ask you to post the code you have.

Instead, implement the following class:

class Buffer
{
private:
// whatever
public:
Buffer();
void add( char const s[] );
std::string result() const;
};

with e.g. the following driver program:

int main()
{
Buffer buf;

buf.add( "Hello, " );
buf.add( "world" );
std::cout << buf.result() << std::endl;
buf.add( "!" );
std::cout << buf.result() << std::endl;
}

which should give the result

Hello, world
Hello, world!

and post your code here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Bob Smith wrote:
I am downloading over http port 80 some contents froma site, but the
contents is not properly stored after using ostringstream for temporary
storage, and later ostringstream:: str() for passing it over to a string.
This doesn't work. It misses data and teh result is not the same as when
using char buffer[bufsize], which gives correct download. Could anyone
point out why the ostringstream + string way doesn't work, while the char
buffer[buffsize] does?
tia


Hi Bob,
Without seeing your actual code it's difficult, but you can try the
following example. Hope it helps.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
ostringstream outputStream;

string resultString;

outputStream << "Text1 ";

outputStream << "Text2 ";

resultString = outputStream.st r();

cout << resultString;

return 0;

}

--

// Morten - www.mortenvp.dk
Jul 23 '05 #3
Alf P. Steinbach wrote:
* Bob Smith:
I am downloading over http port 80 some contents froma site, but the
contents is not properly stored after using ostringstream for temporary
storage, and later ostringstream:: str() for passing it over to a string.
This doesn't work. It misses data and teh result is not the same as when
using char buffer[bufsize], which gives correct download. Could anyone
point out why the ostringstream + string way doesn't work, while the char
buffer[buffsize] does?


It can be related to a bug-ridden download code that has exquisite timing
requirements.

However, it's most likely only related to incorrect usage of
'ostringstream' .

Since I suspect that your code intermingles the aspects of
platform-specific downloading and C++ buffering I won't ask you to post
the code you have.

Instead, implement the following class:

class Buffer
{
private:
// whatever
public:
Buffer();
void add( char const s[] );
std::string result() const;
};

with e.g. the following driver program:

int main()
{
Buffer buf;

buf.add( "Hello, " );
buf.add( "world" );
std::cout << buf.result() << std::endl;
buf.add( "!" );
std::cout << buf.result() << std::endl;
}

which should give the result

Hello, world
Hello, world!

and post your code here.

Thank's alf for the prompt reply. Tested your approach and it works very
well. I made a buf of chars which expands as more data comes available.
Thank you.
<code>

#ifndef _QXRSSBUFFER_H
#define _QXRSSBUFFER_H
class QxRSSBuffer{
public:
QxRSSBuffer( int size, int increment = 512 );
void add( const char * p, int len );
const char * data()const;
~QxRSSBuffer();
int size()const;
private:
char * Buf;
int Size;
int Used;
int Increment;
};
#endif

#include <qxrssbuffer. h>
#include <g++/cstring>
QxRSSBuffer::Qx RSSBuffer( int size, int increment )
:Size( size ), Used ( 0 ), Increment( increment )
{
Buf = new char[ Size ];
memset( Buf, 0, Size );
}
void QxRSSBuffer::ad d( const char * p, int len )
{
if ( ( Used + len ) < Size ) {
memcpy( (void*)&Buf[Used], (void*)p, len );
Used += len;
return;
}
char * buf = new char[ Size + Increment ];
memcpy( buf, Buf, Used );
delete [] Buf;
Buf = buf;
Size += Increment;
add( p, len );
}
const char * QxRSSBuffer::da ta()const{
return Buf;
}
QxRSSBuffer::~Q xRSSBuffer(){
delete [] Buf;
}
int QxRSSBuffer::si ze()const{
return Used;
}

</code>
( improvements welcome )
--
http://www.kolumbus.fi/bob.smith
Jul 23 '05 #4
Bob Smith wrote:
Thank's alf for the prompt reply. Tested your approach and it works very
well. I made a buf of chars which expands as more data comes available.
Thank you.
Why? You like latent bugs and reinventing the wheel? Why not use
std::string or vector?
class QxRSSBuffer{
public:
QxRSSBuffer( int size, int increment = 512 );
void add( const char * p, int len );
const char * data()const;
~QxRSSBuffer();
int size()const;
Your class manages internal memory allocations but lacks a copy
constructor or copy assignment operator (or anyway to specifically
avoid their use). Hence you are asking for trouble.

memcpy( (void*)&Buf[Used], (void*)p, len );
What's with the unnecessary casts above?
char * buf = new char[ Size + Increment ];
memcpy( buf, Buf, Used );
delete [] Buf;
Buf = buf;
Size += Increment;
add( p, len );

Not very efficient if the new size is much larger than the increment.
Jul 23 '05 #5
* Bob Smith:

#include <qxrssbuffer. h>
#include <g++/cstring>
QxRSSBuffer::Qx RSSBuffer( int size, int increment )
:Size( size ), Used ( 0 ), Increment( increment )
{
Buf = new char[ Size ];
memset( Buf, 0, Size );
}
void QxRSSBuffer::ad d( const char * p, int len )
{
if ( ( Used + len ) < Size ) {
memcpy( (void*)&Buf[Used], (void*)p, len );
Used += len;
return;
}
char * buf = new char[ Size + Increment ];
memcpy( buf, Buf, Used );
delete [] Buf;
Buf = buf;
Size += Increment;
add( p, len );
}

There are three main problems with this, that I can see right away.

First, the code assumes that 'len' is always less than 'Increment';
an 'assert' to that effect could help you avoid trouble (buffer overrun),
and a ditto comment in the header file, but better, make no such assumption.

Second, for a small 'Increment' relative to the total final size the
copying involved makes for O(n^2) behavior. The usual way to avoid that
is to double the buffer length each time it's exceeded, which makes for O(n)
time total, and constant amortized time per character. And that's what
e.g. std::vector does, so you can use std::vector as your internal buffer
implementation instead of the low-level and unsafe pointer handling.

Third, of interest if this class is going to be reused, it doesn't define
or declare a copy constructor nor an assignment operator. If you're not
going to support copying of buffers then simply declare a copy constructor
and an assignment operator, as 'private:', and don't define them. As it is,
if an object is copied you have a bug.

Finally, but not really a technical problem per se in the code above, just a
way to avoid potential problems, I recommend using the C++ standard
library's copy operations instead of unsafe memcpy.

( improvements welcome )


See above. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6
Alf P. Steinbach wrote:
First, the code assumes that 'len' is always less than 'Increment';
an 'assert' to that effect could help you avoid trouble (buffer overrun),
and a ditto comment in the header file, but better, make no such assumption.


No it doesn't, it just recursively calls add to increment the string
until the buffer is big enough. Not the greatest idea in the world,
but in this case it works.
Jul 23 '05 #7
Alf P. Steinbach wrote:
* Bob Smith:

#include <qxrssbuffer. h>
#include <g++/cstring>
QxRSSBuffer::Qx RSSBuffer( int size, int increment )
:Size( size ), Used ( 0 ), Increment( increment )
{
Buf = new char[ Size ];
memset( Buf, 0, Size );
}
void QxRSSBuffer::ad d( const char * p, int len )
{
if ( ( Used + len ) < Size ) {
memcpy( (void*)&Buf[Used], (void*)p, len );
Used += len;
return;
}
char * buf = new char[ Size + Increment ];
memcpy( buf, Buf, Used );
delete [] Buf;
Buf = buf;
Size += Increment;
add( p, len );
}

There are three main problems with this, that I can see right away.

First, the code assumes that 'len' is always less than 'Increment';
an 'assert' to that effect could help you avoid trouble (buffer overrun),
and a ditto comment in the header file, but better, make no such
assumption.

Second, for a small 'Increment' relative to the total final size the
copying involved makes for O(n^2) behavior. The usual way to avoid that
is to double the buffer length each time it's exceeded, which makes for
O(n)
time total, and constant amortized time per character. And that's what
e.g. std::vector does, so you can use std::vector as your internal buffer
implementation instead of the low-level and unsafe pointer handling.


hmm, ahaa, ...

Third, of interest if this class is going to be reused, it doesn't define
or declare a copy constructor nor an assignment operator. If you're not
going to support copying of buffers then simply declare a copy constructor
and an assignment operator, as 'private:', and don't define them. As it
is, if an object is copied you have a bug.
implemented.

Finally, but not really a technical problem per se in the code above, just
a way to avoid potential problems, I recommend using the C++ standard
library's copy operations instead of unsafe memcpy.

( improvements welcome )


See above. ;-)

Many thank's Alf, your input is highly appreciated.

:-)

--
http://www.kolumbus.fi/bob.smith
Jul 23 '05 #8
Ron Natalie wrote:
Bob Smith wrote:
Thank's alf for the prompt reply. Tested your approach and it works very
well. I made a buf of chars which expands as more data comes available.
Thank you.
Why? You like latent bugs and reinventing the wheel? Why not use
std::string or vector?


I tried using std::ostringstr eam in concert with std::string to take down
the contents, but the results were not what they should have been. This
could be due to a bug in the stl at my machine, don't know.
class QxRSSBuffer{
public:
QxRSSBuffer( int size, int increment = 512 );
void add( const char * p, int len );
const char * data()const;
~QxRSSBuffer();
int size()const;
Your class manages internal memory allocations but lacks a copy
constructor or copy assignment operator (or anyway to specifically
avoid their use). Hence you are asking for trouble.


implemented.

memcpy( (void*)&Buf[Used], (void*)p, len );


What's with the unnecessary casts above?

removed.
char * buf = new char[ Size + Increment ];
memcpy( buf, Buf, Used );
delete [] Buf;
> Buf = buf;
> Size += Increment;
> add( p, len );
>

Not very efficient if the new size is much larger than the increment.


true, but the increment can be set during object creation.
Can you suggest a better approach for this problem?

to summarize: I download over a socket some contents from a website over
http port 80( in this case an image ) which is of unknown size at the this
objects/class' creation time, and need a buffer for holding the image data.
This works well with the approach I outlined here in this thread, but
failed to operate properly with std::ostringstr eam + std::string.

Your expert advice is highly appreciated.
--
http://www.kolumbus.fi/bob.smith
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4068
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
7
2554
by: SevDer | last post by:
Hi We have a static hashtable that is located in another tier in our n-tiered web application. And we are storing big but not huge objects in this hashtable and the key to the objects is through Session.SessionID. public class DataStore : System.Web.UI.Page { public static Hashtable ht = new Hashtable();
5
28394
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
14
4458
by: cpisz | last post by:
I want to do some find and erase operations that i find in the string class, upon the entire text contents of a file. I made a function that will take a string (designed to hold the entire file contents) my problem now is how can i grab all the file contents in one swoop and put it in a string for editing? I hate to go through the file and copy line by line:
14
1802
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I can do this in notepad and it works fine, but when I start getting in to reading the files I think it has some encoding problem. I tried saving the file with every encoding option. When I open a PDF in the
6
2750
by: Howard | last post by:
I am having a problem that is driving me crazy and I am whits end. I am hoping that someone here can help me. I have a client server application where the client is drawing a progressive bar graph based on data retrieved from the server once a second. The basic loop looks like this; </HEAD> <script language="JavaScript1.2"> var nodedatareq; function buildGraphs() {
3
5092
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages (Java). It stores character strings, and resizes itself to accommodate new strings when needed. Interface: ----------
2
2170
by: =?Utf-8?B?S3VtYXI=?= | last post by:
I am using granados telnet client for connecting to the telnet and get the data from it. Every thing appears to be going smooth. But for some reason when I try to write the byte data to a string or streamwriter, it looses the final packet. Strangely, If I output the datapackets to a console from the telnet server, it perfectly gets all the output packets. the code snippet is below: StreamWriter sw = new StreamWriter("c:\\output.txt",...
6
1606
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I am using a RichTextControl (C# VS2005 .NET 2.0) and allowing users to change the font and color of what ever they select. The problem I am having is that when I select a second set of character and change the font and or color the Font and color from the previous change is lost. This is the code that handles the change private void ChangeFont() { if (InvokeRequired) {
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.