473,908 Members | 7,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istringstream?? ?

bml
Could you help and answer my questions of istringstream? Thanks a lot!

1. Reuse an "istringstr eam"

istringstream ist;
ist.str("This is FIRST test string");

ist.str("This is SECOND test string");
cout << ist.str() << endl;

Is there any problem with the above code?
Is it necessary to add "ist.clear( );" between those two "ist.str()" ?
2. Sub-string of an "istringstr eam"

string str1, str2, str3, str4;
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

How to insert the sub-string, "test", directly into str4, without going
through str1, str2, and str3?
How to remove the counterpart of "str1" from "ist" after "ist >> str1;",
"str1" from "ist" after "ist >> str1;" and so on?
3. Usage of skipws

istringstream ist;
ist.str("This is FIRST test string");
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

ist >> skipws >> str1 ;
ist >> skipws >> str2 ;
ist >> skipws >> str3;
ist >> skipws >> str4;

Adding "skipws" don't change format and content of str1,...str4 at all? So
what's the usage of "skipws"?
4. Why need ostringstream and istringstream separate, instead of combining
them into a single one with both ">>" and "<<"?


Jul 22 '05 #1
3 4747
"bml" <le*****@yahoo. com> wrote...
Could you help and answer my questions of istringstream? Thanks a lot!

1. Reuse an "istringstr eam"

istringstream ist;
ist.str("This is FIRST test string");

ist.str("This is SECOND test string");
cout << ist.str() << endl;

Is there any problem with the above code?
Aside from it being a fragment outside of any function, no.
Is it necessary to add "ist.clear( );" between those two "ist.str()" ?
You mean, between those two "ist.str( <some string literal> )"? No.
2. Sub-string of an "istringstr eam"

string str1, str2, str3, str4;
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

How to insert the sub-string, "test", directly into str4, without going
through str1, str2, and str3?
There is no way. It's a stream. Well, not entirely true, you could
of course, position the sream if you knew where.
How to remove the counterpart of "str1" from "ist" after "ist >> str1;",
"str1" from "ist" after "ist >> str1;" and so on?
What do you mean by "remove"? The stream [buffer] position is changed
after every reading. Why do you need to remove anything?
3. Usage of skipws

istringstream ist;
ist.str("This is FIRST test string");
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

ist >> skipws >> str1 ;
ist >> skipws >> str2 ;
ist >> skipws >> str3;
ist >> skipws >> str4;

Adding "skipws" don't change format and content of str1,...str4 at all? So
what's the usage of "skipws"?
IIRC, it affects only some operations. For example, if you read 'str1' and
then try to extract the _rest_ of the buffer, youi will get "is FIRST..."
instead of " is FIRST...".
4. Why need ostringstream and istringstream separate, instead of combining
them into a single one with both ">>" and "<<"?


I guess I don't understand the question. There is 'std::stringstr eam',
which is bi-directional. Why have std::ostream and std::istream?..

V
Jul 22 '05 #2
bml
Victor Bazarov <v.********@com Acast.net> wrote in message
news:FInRb.1617 76$xy6.780699@a ttbi_s02...
"bml" <le*****@yahoo. com> wrote...
Could you help and answer my questions of istringstream? Thanks a lot!

1. Reuse an "istringstr eam"

istringstream ist;
ist.str("This is FIRST test string");

ist.str("This is SECOND test string");
cout << ist.str() << endl;

Is there any problem with the above code?
Aside from it being a fragment outside of any function, no.
Is it necessary to add "ist.clear( );" between those two "ist.str()" ?


Thank you for your answers! So what's the usage of ist.clear()?

You mean, between those two "ist.str( <some string literal> )"? No.
2. Sub-string of an "istringstr eam"

string str1, str2, str3, str4;
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

How to insert the sub-string, "test", directly into str4, without going
through str1, str2, and str3?


There is no way. It's a stream. Well, not entirely true, you could
of course, position the sream if you knew where.


What do you mean by positioning the stream?

4. Why need ostringstream and istringstream separate, instead of combining them into a single one with both ">>" and "<<"?


I guess I don't understand the question. There is 'std::stringstr eam',
which is bi-directional. Why have std::ostream and std::istream?..

Exactly is it what I wanted to ask.

Jul 22 '05 #3
bml wrote:
2. Sub-string of an "istringstr eam"

string str1, str2, str3, str4;
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

How to insert the sub-string, "test", directly into str4, without going
through str1, str2, and str3?


There is no way. It's a stream. Well, not entirely true, you could
of course, position the sream if you knew where.


What do you mean by positioning the stream?


How old are you? I don't ask because I want to be impolite
or I want to insult you BUT I just want to know if you are old
enough to remember the days of audio casette recorders or tape
drives.

A stream works like a tape. You can only read and write sequentially.
Well. If you know exactly what you are looking for you can press the
button on your tape deck and do a fast forward. But you need to

be carefull, without a tape meter it is practically impossible to
position the tape *exactly* where you want to start. And even then it
is impossible if you don't know the length of each song on the tape.
With a stream its similar: If you know the exact location you can
position the stream there and start reading. But without this knowledge
you are lost. So if there are 4 words in the stream and you do not know
anything about them such as the length of each, there simply is no way to
determine the start of the 4-th word other then read through the previous
words.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #4

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

Similar topics

1
3437
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the allocated memory correctly? Or am I missing something glaringly simple? Thanks in advance, S. Armondi std::istringstream** ArgStream;
8
5381
by: Agent Mulder | last post by:
I try to remove the spaces from a string using an old trick that involves an istringstream object. I expect the while-condition while(istringstream>>string) to evaluate to false once the istringstream is exhausted, but that is not the case. What am I missing? #include<iostream> #include<sstream> #include<string>
7
748
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream encounters the two byte shorts, it either thinks it has reached the null terminator? or eof and consequently stops reading values back in. It doesn't matter whether or not I use the std::ios::binary flag when opening the istringstream or the...
4
2726
by: Brandon | last post by:
I'm using an istringstream to convert some integers stored in string form to integers. However, each integer is stored in a differnt string. So, I used istringstream's str(string) method for the first string and then used the >> operator to extract the integer. It worked just fine. So, on to the second string. I used str(string) again but this time extraction gave me a seemingly random number (-858993460). Once an istringstream is...
4
2339
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output data. The data comes in string lines like "OREBlegQ 14854 731.818" which need to be split into a string, int and double when stored in the class. Can anyone help? This is what i have so far: /* Begin Code */ #include <sstream>
6
2380
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be unmodified... However when I try to pass it fn(istringstream s) // doesn't compile but fn(istringstream & s) // does.
8
2104
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
3022
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
11
2923
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
0
10031
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
11337
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
10536
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
9721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8094
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
5930
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...
1
4770
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
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3355
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.