char* to string

G

gipsy boy

Silly problem, but it's going to teach me something cause I'm confused..
How do I store the letters of a char buf[12] into a string?
Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;
cout << birthDateStr << l << endl;

Now, this works, but when I exit the scope (birthDateStr is a class
member), birthDateStr is suddenly empty again! Why?

If you have a better way (perhaps with a temporary char * instead of a
heaped array), please explain it to me?

thanks,
 
K

Karl Heinz Buchegger

gipsy said:
Silly problem, but it's going to teach me something cause I'm confused..
How do I store the letters of a char buf[12] into a string?
Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;


Why so complicated?
I assume borthDateStr is of type std::string

birthDateStr = buf;
 
M

msalters

gipsy said:
Silly problem, but it's going to teach me something cause I'm confused..
How do I store the letters of a char buf[12] into a string?

Use the std::string ctor which takes a char const*
Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;
cout << birthDateStr << l << endl;

Now, this works, but when I exit the scope (birthDateStr is a class
member), birthDateStr is suddenly empty again! Why?


I suspect you have not resized birthDateStr, so there's no space
allocated. Another option is that you're looking at a copy of
birthDateStr. We can't be sure of course. You write "when I exit
the scope" without showing that scope!

Anyway, what was wrong with birthDateStr=buf; ?
Regards,
Michiel Salters
 
G

gipsy boy

Karl said:
gipsy said:
Silly problem, but it's going to teach me something cause I'm confused..
How do I store the letters of a char buf[12] into a string?
Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;



Why so complicated?
I assume borthDateStr is of type std::string

birthDateStr = buf;


I made it so complicated because this doesn't work either.
When I do what you propose, birthDateStr is nothing anymore when I exit
the scope of the assignment function.
 
G

gipsy boy

msalters said:
gipsy said:
Silly problem, but it's going to teach me something cause I'm
confused..

How do I store the letters of a char buf[12] into a string?


Use the std::string ctor which takes a char const*

Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;
cout << birthDateStr << l << endl;

Now, this works, but when I exit the scope (birthDateStr is a class
member), birthDateStr is suddenly empty again! Why?



I suspect you have not resized birthDateStr, so there's no space
allocated. Another option is that you're looking at a copy of
birthDateStr. We can't be sure of course. You write "when I exit
the scope" without showing that scope!

Anyway, what was wrong with birthDateStr=buf; ?


Maybe it's because the function I assign birthDateStr with is an inline
constructor? Let's try, maybe that's the problem..
There si only one birthDateStr, it's a member of the class, type string.
 
K

Karl Heinz Buchegger

gipsy said:
gipsy said:
Silly problem, but it's going to teach me something cause I'm confused..
How do I store the letters of a char buf[12] into a string?
Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;



Why so complicated?
I assume borthDateStr is of type std::string

birthDateStr = buf;


I made it so complicated because this doesn't work either.


Then you have another problem.
Post your real code. Best would be a complete, compilable
program, that shows your problem.
 
K

Karl Heinz Buchegger

gipsy said:
gipsy said:
Silly problem, but it's going to teach me something cause I'm
confused..

How do I store the letters of a char buf[12] into a string?


Use the std::string ctor which takes a char const*

Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;
cout << birthDateStr << l << endl;

Now, this works, but when I exit the scope (birthDateStr is a class
member), birthDateStr is suddenly empty again! Why?



I suspect you have not resized birthDateStr, so there's no space
allocated. Another option is that you're looking at a copy of
birthDateStr. We can't be sure of course. You write "when I exit
the scope" without showing that scope!

Anyway, what was wrong with birthDateStr=buf; ?


Maybe it's because the function I assign birthDateStr with is an inline
constructor? Let's try, maybe that's the problem..
There si only one birthDateStr, it's a member of the class, type string.


Show at least the complete function and class declaration.
Otherwise everybody has to guess what you might have done.
 
G

gipsy boy

Karl said:
gipsy said:
msalters said:
gipsy boy wrote:


Silly problem, but it's going to teach me something cause I'm

confused..


How do I store the letters of a char buf[12] into a string?


Use the std::string ctor which takes a char const*



Whenever I do this, when buf exists the scope of its creation, the
string data is lost.

This is the current code :

char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
for(int i=0;i<l;i++) birthDateStr = buf;
cout << birthDateStr << l << endl;

Now, this works, but when I exit the scope (birthDateStr is a class
member), birthDateStr is suddenly empty again! Why?


I suspect you have not resized birthDateStr, so there's no space
allocated. Another option is that you're looking at a copy of
birthDateStr. We can't be sure of course. You write "when I exit
the scope" without showing that scope!

Anyway, what was wrong with birthDateStr=buf; ?


Maybe it's because the function I assign birthDateStr with is an inline
constructor? Let's try, maybe that's the problem..
There si only one birthDateStr, it's a member of the class, type string.



Show at least the complete function and class declaration.
Otherwise everybody has to guess what you might have done.


in a truncated form, it's..

class Candidate {
public:
string birthDateStr;
tm birthDate;

Candidate(string birthDateStr,int birthDay,int birthMonth...) {
memset(&birthDate,0,sizeof(tm));
birthDate.tm_year = birthYear;
birthDate.tm_mon = birthMonth;
birthDate.tm_mday = birthDay;
char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
birthDateStr = buf;
cout << birthDateStr << endl;

// this outputs the correct date!
}

Candidate(const Candidate & x) {
this->birthDayStr = x.birthDayStr;
etc ...
}

string getBirthDateStr() const { return birthDateStr; }

// Operators for streaming in XML
friend ostream& operator << (ostream& os, const IdoolCandidate&
candidate);

}

There is also a copy constructor that

Now, when I call this getBirthDateStr(), inside the xml streaming
operator. (so from the reference candidate) It seems that its
birthDateStr is "".

Why is that?
 
H

Howard

Here's your problem:
Candidate(string birthDateStr,int birthDay,int birthMonth...) {
memset(&birthDate,0,sizeof(tm));
birthDate.tm_year = birthYear;
birthDate.tm_mon = birthMonth;
birthDate.tm_mday = birthDay;
char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
birthDateStr = buf;
cout << birthDateStr << endl;

// this outputs the correct date!
}

You stated you had only one birthDateStr, but this constructor has a
parameter with the same name! Why do you have that parameter there at all,
if you're trying to set the member variable based on other info? Either
drop the parameter, or rename it (and use it correctly).

-Howard
 
G

gipsy boy

Howard said:
Here's your problem:

Candidate(string birthDateStr,int birthDay,int birthMonth...) {
memset(&birthDate,0,sizeof(tm));
birthDate.tm_year = birthYear;
birthDate.tm_mon = birthMonth;
birthDate.tm_mday = birthDay;
char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
birthDateStr = buf;
cout << birthDateStr << endl;

// this outputs the correct date!
}


You stated you had only one birthDateStr, but this constructor has a
parameter with the same name! Why do you have that parameter there at all,
if you're trying to set the member variable based on other info? Either
drop the parameter, or rename it (and use it correctly).

No sorry, that was a mistake, there is no such parameter. Please ignore
it..I was just about to correct that post.
 
G

gipsy boy

gipsy said:
Howard said:
Here's your problem:

Candidate(string birthDateStr,int birthDay,int birthMonth...) {
memset(&birthDate,0,sizeof(tm));
birthDate.tm_year = birthYear;
birthDate.tm_mon = birthMonth;
birthDate.tm_mday = birthDay;
char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
birthDateStr = buf;
cout << birthDateStr << endl;

// this outputs the correct date!
}


You stated you had only one birthDateStr, but this constructor has a
parameter with the same name! Why do you have that parameter there at
all, if you're trying to set the member variable based on other info?
Either drop the parameter, or rename it (and use it correctly).


No sorry, that was a mistake, there is no such parameter. Please ignore
it..I was just about to correct that post.

What?... My code works now, when I remove the line that prints
birthDateStr to STDOUT. Does anybody have *any* explanation for that?
 
H

Howard

gipsy boy said:
gipsy said:
Howard said:
Here's your problem:


Candidate(string birthDateStr,int birthDay,int birthMonth...) {
memset(&birthDate,0,sizeof(tm));
birthDate.tm_year = birthYear;
birthDate.tm_mon = birthMonth;
birthDate.tm_mday = birthDay;
char buf[12];
int l = strftime(buf,12,"%d/%m/%y",&birthDate);
birthDateStr = buf;
cout << birthDateStr << endl;

// this outputs the correct date!
}



You stated you had only one birthDateStr, but this constructor has a
parameter with the same name! Why do you have that parameter there at
all, if you're trying to set the member variable based on other info?
Either drop the parameter, or rename it (and use it correctly).


No sorry, that was a mistake, there is no such parameter. Please ignore
it..I was just about to correct that post.

What?... My code works now, when I remove the line that prints
birthDateStr to STDOUT. Does anybody have *any* explanation for that?

Not without REAL code. I see you referring to "birthDayStr", which I don't
see defined, and that your streaming operator takes a IdoolCandidate
parameter, but I have no idea what that is, either. If you're trying to
replicate the code you really have, but not post your actual (private) code,
then you might try creating (and compiling and testing) a small test app
that shows the same problem, and post that. At least it will be real,
working code that we can comment on.

-Howard
 
R

Ron Natalie

gipsy said:
I made it so complicated because this doesn't work either.

It should if l is less than 12 in your example (otherwise stftime won't
null terminate it).
When I do what you propose, birthDateStr is nothing anymore when I exit
the scope of the assignment function.

Assigning a char* to a std::string COPIES the data up to the null terminator.
You can also use
birthDateStr.assign(birthDateStr, l)
to cover the overflow case.

provided that birthDateStr is a std::string, it is unaffected by the right
hand side of the assignment changing once the assignment is done.

I think you have more problem than you are showing...how about a more
complete example.
 
G

gipsy boy

Ron said:
It should if l is less than 12 in your example (otherwise stftime won't
null terminate it).


Assigning a char* to a std::string COPIES the data up to the null
terminator.
You can also use
birthDateStr.assign(birthDateStr, l)
to cover the overflow case.

provided that birthDateStr is a std::string, it is unaffected by the right
hand side of the assignment changing once the assignment is done.

I think you have more problem than you are showing...how about a more
complete example.

Frankly I think it was a problem with kdevelop lying to me about
recompiling my project. And my whole computer's acting funny now.. Let
it rest, it was probably a non-existant problem..I'm just under a bit of
stress.
 

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

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top