473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

char* to string

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
13 7260
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

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

"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
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same...
24
by: Norm | last post by:
Could someone explain for me what is happening here? char *string; string = new char; string = "This is the string"; cout << string << std::endl; The output contains (This the string) even...
9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
4
by: Radde | last post by:
Hi, class String { public: String(char* str); void operator char*();
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.