junk information an the end of dumping out string to a file.

Y

yinglcs

I have a class which has an attribute 'path' of type 'string'.

class MyClass {
public:
MyClass(const string& parentpath, int count) ;
void dump(ostream& os) const;

private:
string path;
int type;
}


MyClass::MyClass(const string& parentpath, int count) {
type = // a function to find out type;
ostringstream apath;

apath << parentpath;
apath << "/";
apath << count;

path = apath.str();
}

void MyClass::dump(ostream& os) const {

os << " type:" << type;
os << " path:" << path;
os << endl;

}

In some case, not always , I this print out junk at the end of 'path',

type:3 path:/-/3/3/3/0/0^@^@^@^@1^@^@^@^@#b^Hh^LU^^AA

Can you please tell me what did I do wrong?

Thank you.
 
V

Victor Bazarov

I have a class which has an attribute 'path' of type 'string'.

class MyClass {
public:
MyClass(const string& parentpath, int count) ;
void dump(ostream& os) const;

private:
string path;
int type;
}


MyClass::MyClass(const string& parentpath, int count) {
type = // a function to find out type;
ostringstream apath;

apath << parentpath;
apath << "/";
apath << count;

path = apath.str();
}

void MyClass::dump(ostream& os) const {

os << " type:" << type;
os << " path:" << path;
os << endl;

}

In some case, not always , I this print out junk at the end of 'path',

type:3 path:/-/3/3/3/0/0^@^@^@^@1^@^@^@^@#b^Hh^LU^^AA

Can you please tell me what did I do wrong?


You didn't post real code. Post real code, then we can talk.

V
 
B

benben

I have a class which has an attribute 'path' of type 'string'.

class MyClass {
public:
MyClass(const string& parentpath, int count) ;
void dump(ostream& os) const;

private:
string path;
int type;
}

Please qualify string and ostream with std::
Missing semicolon.
MyClass::MyClass(const string& parentpath, int count) {
type = // a function to find out type;

What's that you want to find with type?
ostringstream apath;

apath << parentpath;
apath << "/";
apath << count;

path = apath.str();
}

void MyClass::dump(ostream& os) const {

os << " type:" << type;
os << " path:" << path;
os << endl;

}

In some case, not always , I this print out junk at the end of 'path',

type:3 path:/-/3/3/3/0/0^@^@^@^@1^@^@^@^@#b^Hh^LU^^AA

Can you please tell me what did I do wrong?

Well, are you sure it is the part that you posted that has contains the
error?

To really help us help yourself perhaps you should post a complete
program that demonstrates the problem. It is hard to guess in what
context is your class used and under that condition does the error pop up.

But before doing so, you may like to run your debugger, check what's
passed in MyClass constructor in the first place.
Thank you.

Regards,
Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top