By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,344 Members | 1,806 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,344 IT Pros & Developers. It's quick & easy.

char* to string

P: n/a
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[i] = buf[i];
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,

--
- gipsy boy
Jul 22 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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?
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[i] = buf[i];
Why so complicated?
I assume borthDateStr is of type std::string

birthDateStr = buf;
cout << birthDateStr << l << endl;


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a

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[i] = buf[i];
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

Jul 22 '05 #3

P: n/a
Karl Heinz Buchegger wrote:
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?
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[i] = buf[i];

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.

--
- gipsy boy
Jul 22 '05 #4

P: n/a
msalters wrote:
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[i] = buf[i];
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.

--
- gipsy boy
Jul 22 '05 #5

P: n/a
gipsy boy wrote:

Karl Heinz Buchegger wrote:
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?
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[i] = buf[i];

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

P: n/a
gipsy boy wrote:

msalters wrote:
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[i] = buf[i];
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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7

P: n/a
Karl Heinz Buchegger wrote:
gipsy boy wrote:
msalters wrote:
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[i] = buf[i];
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?
--
- gipsy boy
Jul 22 '05 #8

P: n/a

"gipsy boy" <x@x.pi> wrote in message
news:Z5*********************@phobos.telenet-ops.be...

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
Jul 22 '05 #9

P: n/a
Howard wrote:
"gipsy boy" <x@x.pi> wrote in message
news:Z5*********************@phobos.telenet-ops.be...

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.

--
- gipsy boy
Jul 22 '05 #10

P: n/a
gipsy boy wrote:
Howard wrote:
"gipsy boy" <x@x.pi> wrote in message
news:Z5*********************@phobos.telenet-ops.be...

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?

--
- gipsy boy
Jul 22 '05 #11

P: n/a

"gipsy boy" <x@x.pi> wrote in message
news:uh*********************@phobos.telenet-ops.be...
gipsy boy wrote:
Howard wrote:
"gipsy boy" <x@x.pi> wrote in message
news:Z5*********************@phobos.telenet-ops.be...

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?

--
- gipsy boy


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
Jul 22 '05 #12

P: n/a
gipsy boy wrote:
birthDateStr = buf;

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.
Jul 22 '05 #13

P: n/a
Ron Natalie wrote:
gipsy boy wrote:
birthDateStr = buf;


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.


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.

--
Maarten
--
- gipsy boy
Jul 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.