454,344 Members | 1,806 Online
+ 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
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

 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

 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, thestring 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

 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, thestring 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

 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, thestring 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

 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, thestring 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

 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'mconfused.. 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, thestring data is lost.This is the current code : char buf[12]; int l = strftime(buf,12,"%d/%m/%y",&birthDate); for(int i=0;ibirthDayStr = 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" 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" 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" 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" wrote in message news:uh*********************@phobos.telenet-ops.be... gipsy boy wrote: Howard wrote: "gipsy boy" 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.