As an exercise, I'm trying to create a input stream manipulator that will suck up characters and put them in a string until it encounters a specific character or until it reaches eof. The idea came from Bruce Eckel's 'Thinking in c++' page 249.
Here's the code I have so far:
#include <string>
#include <iostream>
#include <istream>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
class siu
{
char T;
string *S;
public:
siu (string *s, char t)
{
T = t;
S = s;
*S = "";
}
friend istream& operator>>(istream& is, siu& SIU)
{
char N;
bool done=false;
while (!done)
{
is >> N;
if ((N == SIU.T) || is.eof())
done = true;
else
SIU.S->append(&N);
}
return is;
}
};
and to test it....
{
istringstream iss("1 2 now is the time for all/");
int a,b;
string stuff, zork;
iss >> a >> b >> siu(&stuff,'/');
zork = stuff;
}
the idea being that siu(&stuff,'/') will suck up characters from iss until it encounters the /. I can watch it with the debugger as it gets the characters 'n' 'o' 'w' through '/' and terminates the loop. It all seems to be going swimingly until I look at Stuff. Stuff has the characters now etc BUT there are 6 extra characters between each of them. Here's a sample:
- &stuff 0x0012fba4 {0x008c1861 "nÌÌÌýoÌÌÌýwÌÌÌýiÌÌÌýsÌÌÌýtÌÌÌýhÌÌÌýeÌÌÌýtÌÌÌýiÌÌÌýmÌÌÌýeÌÌÌýfÌÌÌýoÌÌÌýrÌÌÌýaÌÌÌýlÌÌÌýlÌÌÌý"}
What's going on?