char * to string [英] char* to string
问题描述
愚蠢的问题,但它会告诉我一些因为我很困惑的事情。
如何将char buf [12]的字母存储到字符串中?
每当我这样做时,当buf存在其创建范围时,
字符串数据就会丢失。
这是当前代码:
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;
现在,这可行,但是当我退出范围(birthDateStr是一个类
成员)时,birthDateStr再次突然变空!为什么?
如果你有更好的方法(也许有一个临时的char *而不是一个
堆积的数组),请向我解释一下?
谢谢,
-
- 吉普赛男孩
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
推荐答案
gipsy boy写道:
gipsy boy wrote:
愚蠢的问题,但它会教我一些因为我很困惑......
将char buf [12]的字母存储到字符串中?
每当我这样做时,当buf存在其创建范围时,
字符串数据就会丢失。
char buf [12];
int l = strftime(buf,12,"%d /%m /%y"& birthDate );;
for(int i = 0; i< l; i ++)birthDateStr [i] = buf [i];
为什么这么复杂?
我认为borthDateStr的类型是std :: string
cout<< birthDateStr<< l<< endl;
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
gipsy boy写道:
gipsy boy wrote:
愚蠢的问题,但是它会告诉我一些因为我是b $ b困惑的问题..如何将char buf [12]的字母存储到字符串中?
使用带有char const的std :: string ctor *
每当我这样做时,当buf存在其创建范围时,
字符串数据丢失。
这是当前的代码:
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;
现在,这有效,但是当我退出范围(birthDateStr是一个类
成员)时,birthDateStr再次突然变空!为什么?
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?
我怀疑你没有调整birthDateStr的大小,所以没有分配空间
。另一个选择是你正在查看
birthDateStr的副本。我们当然不能肯定。你写当我退出
范围时没有显示范围!
无论如何,birthDateStr = buf出了什么问题; ?
问候,
Michiel Salters
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
Karl Heinz Buchegger写道:
Karl Heinz Buchegger wrote:
gipsy boy写道:
gipsy boy wrote:
愚蠢的问题,但它会教我一些因为我很困惑的事情......
我该怎么存储这些字母将char buf [12]变成字符串?
每当我这样做时,当buf存在其创建范围时,
字符串数据就会丢失。
这是当前代码:
char buf [12];
int l = strftime(buf,12,"%d /%m /%y",& birthDate);
for(int i = 0; i< l; i ++)birthDateStr [i] = buf [i];
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];
为什么这么复杂?
我认为borthDateStr属于类型std :: string
birthDateStr = buf;
Why so complicated?
I assume borthDateStr is of type std::string
birthDateStr = buf;
我这么复杂,因为这也不起作用。
当我按照你的建议行事时,当我退出
$ b时,birthDateStr就不再是了$ b分配功能的范围。
-
- 吉普赛男孩
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
这篇关于char * to string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!