#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Remove last character from stream


    Hi guys.
    I'm trying to remove the last character from an ostream using \b (backspace):

    Code:
    class A
    {
    public:
    	string m_str;
    	A(string str="None"):m_str(str){}
    	~A(){}
    };
    
    
    
    ostream& operator<<(ostream& os, const A object)
    {
    	return os<<object.m_str<<'\b';
    }
    
    
    int main()
    {
    	A anA("SomeString!");
    	cout<<num;
    }
    but that doesn't seem to work...
    I get this weird output:

    (had to picture it since it can't be copied)
    how can I ignore/erase the last character properly, and why doesn't '\b' work?

    Thanks in advanced!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Inserting more characters onto the output stream makes...more.

    If you need to write some data and later retract some of it, collect the data to an internal buffer before showing it. Where you now print '\b' your buffer filling routine would decrement the position pointer---this appears to be what you're asking for.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Inserting more characters onto the output stream makes...more.

    If you need to write some data and later retract some of it, collect the data to an internal buffer before showing it. Where you now print '\b' your buffer filling routine would decrement the position pointer---this appears to be what you're asking for.
    Hi b49P23TIvg.

    what internal buffer should I use? can ostream be transfer to ostringstream?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    implementation of my previous post this thread


    Code:
    #include<vector>
    #include<iostream>
    
    void stash(std::vector<char>&buffer, char c) {
      if (buffer.size() && ('\b' == c))
        buffer.pop_back();
      else
        buffer.push_back(c);
    }
    
    void show(std::vector<char>buffer) {
      int i, n;
      n = buffer.size();
      for (i = 0; i < n; ++i)
        std::cout << buffer[i];
    }
    
    int main() {
      std::vector<char>buffer;
      stash(buffer, 'b'), stash(buffer, 'u'), stash(buffer, '\b'), stash(buffer, 's'), stash(buffer, '\n');
      show(buffer);
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo