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

Home Posts Topics Members FAQ

stringstream question


class myClass
{
stringstream ioss;
istream* is;
void f()
{
....
string initStr("");
ioss.str( initStr);
ioss.seekg( 0);
ioss.clear();
is->get( *ioss.rdbuf());
.....
}

public:
myClass( istream* is_): ioss(), is( is_) {}

....
};

works well, but:

void f()
{
....
// string initStr("");
ioss.str( ""); // HERE
ioss.seekg( 0);
ioss.clear();
is->get( *ioss.rdbuf());
.....
}
causes the program reproducible to fail.
Is there something obvious wrong? (compiles without warning)
Is there a easier/better/clearer way to reset a stringstream?

thanks,
marc

Jul 19 '05 #1
2 3348
On Tue, 04 Nov 2003 12:03:48 +0900, Marc Schellens
<m_*********@ho tmail.com> wrote:

class myClass
{
stringstream ioss;
istream* is;
void f()
{
...
string initStr("");
ioss.str( initStr);
ioss.seekg( 0);
ioss.clear();
is->get( *ioss.rdbuf());
....
}

public:
myClass( istream* is_): ioss(), is( is_) {}

...
};

works well, but:

void f()
{
...
// string initStr("");
ioss.str( ""); // HERE
ioss.seekg( 0);
ioss.clear();
is->get( *ioss.rdbuf());
....
}
causes the program reproducible to fail.
Is there something obvious wrong? (compiles without warning)
Is there a easier/better/clearer way to reset a stringstream?

thanks,
marc


ioss.rdbuf()->pubseekpos(0,i os_base::in | ios_base::out);

--
grzegorz
Jul 19 '05 #2
Grzegorz Sakrejda wrote:
On Tue, 04 Nov 2003 12:03:48 +0900, Marc Schellens
<m_*********@ho tmail.com> wrote:

class myClass
{
stringstream ioss;
istream* is;
void f()
{
...
string initStr("");
ioss.str( initStr);
ioss.seekg( 0);
ioss.clear();
is->get( *ioss.rdbuf());
....
}

public:
myClass( istream* is_): ioss(), is( is_) {}

...
};

works well, but:

void f()
{
...
// string initStr("");
ioss.str( ""); // HERE
ioss.seekg( 0);
ioss.clear();
is->get( *ioss.rdbuf());
....
}
causes the program reproducible to fail.
Is there something obvious wrong? (compiles without warning)
Is there a easier/better/clearer way to reset a stringstream?

thanks,
marc


ioss.rdbuf()->pubseekpos(0,i os_base::in | ios_base::out);


Thank you, but it does not work.
The buffer is overridden, but the contents is kept:

FMTIn::GetLine: 1.
FMTIn::GetLine: 2.34.
FMTIn::GetLine: 5.34.

The stream (is) was:
1
2.34
5


Jul 19 '05 #3

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

Similar topics

5
15970
by: Ellarco | last post by:
Im sorry for asking a question that is surely in the archives somewhere, but I have been unable to locate it. Its about string memory management. I need to dynamically construct a C-style string and was planning to go about it this way; char* foo() { std::stringstream ss; ss << ...
3
14815
by: Mike Chirico | last post by:
Sorry about the newbie question. What's the best way to convert numbers to strings in C++. The following works, but is it better than using the sprintf() "old C" way of converting? #include <iostream> #include <string> #include <sstream> #include <list>
6
7970
by: Christopher Benson-Manica | last post by:
This is a subtle variation of a question I posted some time ago. #include <map> #include <iostream> #include <sstream> int main(void) { try { std::stringstream s;
5
10898
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ; string str ; cin >> str ; ss << str ;
5
817
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears flags (bits) in the stream, right? I solved it by declaring the stringstream inside the loop body even though I have need of another stringstream just after the loop. So for each iteration the constructor of the stringstream will be executed. Is...
3
5293
by: mlm | last post by:
Hi, I believe that it should not be legal to create a vector (and array) of stringstream since the copy constructor and assignment operators are declared private. However, I don't understand why it shouldn't be legal to create a vector (or array) or stringstream pointers. The following code compiles and runs on g++, but seg faults while using the Intel c++ compiler. Is this standard code? Which newsgroup (intel or g++) needs to be...
9
5526
by: martinezfive | last post by:
Hi, I feel no doubt some documentation contains my answer, so bare with me. Given the following: #inclde <stdio.h> #include <sstream> void f() { std::stringstream a("Hello World!\n");
2
456
by: DaLoverhino | last post by:
I'm trying to get use to using stringstream and I've run across a case that puzzles me. Perhaps you have some suggestion for the following: Why in the code below for case 2, intVal is always 1? // StringToInt.cc // // #include <sstream>
0
1981
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing a LOT of synchronization overhead. After a bit of digging I concluded that this synchronization has to do with the access to a global locale inside the stream. The problem can be seen by running the small distilled benchmark code
1
1471
by: jjblt206 | last post by:
Hi, I have a simple nit-picky question. It would be nice for a stringstream to have a member function which returns a C style string (just like a string does) so I could do this, stringstream ss; ss << "Etc..."; ss.c_str() instead of this,
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
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.