Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 23rd, 2006, 4:58 AM   #1
yOURsELF
Newbie
 
Join Date: Mar 2006
Posts: 3
Rep Power: 0 yOURsELF is on a distinguished road
Remove last char from a stringstream

hi guys, how do i remove the last char from a stringstrem ?
for example: i have this stringstream "ABCD" and i want it to be "ABC"
p.s: i'm using VC++
yOURsELF is offline   Reply With Quote
Old Mar 23rd, 2006, 5:12 AM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4 Klarre is on a distinguished road
If you use the std::stringstream as an std::string instead, you can use this piece of code. It might be a very ugly way to do it. But it works!
#include <iostream>
#include <sstream>

std::string removeLast(std::string x)
{
	std::string y;

	for(std::string::iterator i = x.begin(); i != x.end()-1; ++i)
		y.push_back(*i);

	return y;
}

int main()
{
	std::string x = "ABC";

	std::cout << removeLast(x);

	std::cin.get();

	return 0;
}
This std::string you got returned can you then assign to your std::stringstream.

/Klarre
Klarre is offline   Reply With Quote
Old Mar 23rd, 2006, 6:13 AM   #3
yOURsELF
Newbie
 
Join Date: Mar 2006
Posts: 3
Rep Power: 0 yOURsELF is on a distinguished road
thanks, but i really need to work only with stringstreams any other way ?
yOURsELF is offline   Reply With Quote
Old Mar 23rd, 2006, 6:25 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
As far as I know, you can't. One treats stringstreams as if they were input/output streams.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 23rd, 2006, 7:02 AM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Klarre
If you use the std::stringstream as an std::string instead, you can use this piece of code. It might be a very ugly way to do it. But it works!
 ...
This std::string you got returned can you then assign to your std::stringstream.

/Klarre
Why don't you just use the substr function, it costs less code .
Polyphemus_ is offline   Reply With Quote
Old Mar 23rd, 2006, 7:18 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
#include <iostream>
#include <strstream>
#include <string>
#include <string.h>

using namespace std;

int uhOh (string TroubleInRiverCity)
{
    cerr << TroubleInRiverCity << endl;
    return 1;
}
int main (int argc, char *argv [])
{
    strstream elStreamo;
    elStreamo << "ABCD" << '\0';
    cout << elStreamo.str () << endl;
    int len = strlen (elStreamo.str ());
    elStreamo.str ()[len-1] = '\0';
    cout << elStreamo.str () << endl;
    cin.get ();
    return 0;
}
Quote:
Originally Posted by Output
ABCD
ABC
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 23rd, 2006, 7:30 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Mind you, that you are still using strings by using the str() function.
I don't know why in the world you said:
Quote:
Originally Posted by yOURsELF
thanks, but i really need to work only with stringstreams any other way ?
Btw David, strstream is deprecated, use sstream instead. :p
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 23rd, 2006, 8:45 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Ahhh, well, I'm old and the OP got an answer. Possibly he doesn't realize the contents of a string stream contain a char array or a C-string or a C++ string, depending upon how you go about it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 23rd, 2006, 10:25 AM   #9
yOURsELF
Newbie
 
Join Date: Mar 2006
Posts: 3
Rep Power: 0 yOURsELF is on a distinguished road
i want to thank all of you for your help
now i have one more request (the last i hope). Can someone show me a function that returns this:
example: in (string) -> BDAADXXC out (string) -> BDAC
the function would look for 'X' and would erase the number of chars right before the 'X's.

appreciate for your attention
yOURsELF is offline   Reply With Quote
Old Mar 23rd, 2006, 10:29 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I would suggest (as been earlier suggested) that you're designing your thangy the hard way. I would not lead you further down that path. Perhaps you'd like to elucidate on your goal so that you might get more effective help, instead of just workable help.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC