![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 3
Rep Power: 0
![]() |
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++ |
|
|
|
|
|
#2 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
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;
}/Klarre |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 3
Rep Power: 0
![]() |
thanks, but i really need to work only with stringstreams
any other way ? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
. |
|
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
#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:
__________________
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 |
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Mind you, that you are still using strings by using the str() function.
I don't know why in the world you said: Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Mar 2006
Posts: 3
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|