以下功能有问题 [英] Problem with following function

查看:51
本文介绍了以下功能有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下功能的问题,我一直绞尽脑汁想要找出我出错的地方。我需要做的是

返回一个带有当前日期和时间的格式化字符串,格式为:


[Thu May 5 17:06:27 2005年]


这是我现在的功能:


char * currenttime(){

char *时间;

time_t t;

char * p,* e;

t = time(NULL);

p = ctime(& t);

if(p&&((e = strchr(p,''\ n''))))* e =''\ 0 '';

curtime = malloc(strlen(p)+2);

if(curtime == NULL){

printf(" ;内存不足\ n");

退出(1);

}

snprintf(curtime,sizeof(curtime + 2) ,%c%s%c,'['',p,'']'');

printf("%s \ n",curtime);

返回时间;

}


我希望有人可以帮助我。

-

物化

perl -e''printf%silto%c%sal%c%s%ccodegurus%corg%c,ma,58,mw,107,

呃,64,46,10,

巴特:"什么的圣诞老人的小助手在做那条狗?看起来他是想要跳过来的,但是他不能完全成功。

解决方案

< blockquote> Materialized写道:

我遇到了以下功能的问题,我一直绞尽脑汁b $ b我的大脑试图弄清楚我哪里出错了。我需要做什么
返回一个格式化的字符串,其中包含当前日期和时间的
格式:
[2005年5月5日17:06:27] [
]这是我现在的功能:

char * currenttime(){
char * curtime;
time_t t;
char * p,* e;
t =时间(NULL);
p = ctime(& t);
if(p&&((e = strchr(p,''\ n''' ))))* e =''\''';
curtime = malloc(strlen(p)+2);
if(curtime == NULL){
printf(" ;内存不足\ n");
退出(1);
}
snprintf(curtime,sizeof(curtime + 2),"%c%s%c",' '['',p,'']'');
printf("%s\ n",curtime);
返回时间;
}




sizeof(时间+2)是奇数用法。这意味着与sizeof(curtime)相同我猜测,因为时间+ 2的类型与curtime的类型相同?

无论如何,它会产生数字字节指向char的指针占用你系统的
,通常是4位32位系统,但不一定是。


你想要更像这样的东西:

size_t len = strlen(p)+3; / *每个字符允许1个,1个表示''\ 0''* /

curtime = malloc(len);

....

snprintf(curtime,len,"%c%s%c",''['',p,'']'');


-David


Materialized写道:

我遇到了以下功能的问题,我一直在绞尽脑汁
大脑试图弄清楚我哪里出错了。我需要做的是
返回一个格式化的字符串,其中包含当前日期和时间,格式为:

[2005年5月5日17:06:27]

char * currenttime(){
char * curtime;
time_t t;
char * p,* e;
t =时间(NULL);
p = ctime(& t);
if(p&&((e = strchr(p,''\ n''' ))))* e =''\''';
curtime = malloc(strlen(p)+2);
if(curtime == NULL){
printf(" ;内存不足\ n");
退出(1);
}
snprintf(curtime,sizeof(curtime + 2),"%c%s%c",' '['',p,'']'');
你的问题:sizeof(curtime + 2)

说明:

curtime是一个指针。

sizeof(curtime)是_pointer_的大小。

您想要(strlen(p)+ 2)而不是。


printf("%s \ n",curtime) ;
返回时间;
}
我希望有人可以帮助我。



-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题:< a rel =nofollowhref =http://www.eskimo.com/~scs/c-faq/top.htmltarget =_ blank> http://www.eskimo.com/~scs/c -faq / top.html

alt.comp.lang.learn.c-c ++ faq:
h ttp://www.comeaucomputing.com/learn/faq/

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板库


In comp.lang.c具体化< Ma ********** @ privacy.net>写道:

我遇到了以下功能的问题,我一直绞尽脑汁想要找出错误的地方。我需要做的是
返回一个格式化的字符串,其格式为:
[2005年5月5日17:06:27]
这是我的函数,因为它看台:
char * currenttime(){
char * curtime;
time_t t;
char * p,* e;
t = time(NULL);
p = ctime(& t);
if(p&&((e = strchr(p,''\ n''))))* e =''\ 0 '';


第一个问题:''e''是一个未初始化的指针,所以你不能存储

当前指向的地方。但是因为''e''永远不会被使用,所以你可以简单地删除整行。

curtime = malloc(strlen(p)+2);


因为你要在''curtime''中存储一个字符串,你需要一个

更多的字符用于尾随''\0''。你必须使用


curtime = malloc(strlen(p)+ 3);

if(curtime == NULL){
printf( out of memory\\\
);
exit(1);
}
snprintf(curtime,sizeof(curtime + 2),"%c%s%c", ''['',p,'']'');


''curtime''是一个字符指针,所以''sizeof(curtime)''以及

''toforofof(curtime + 2) ''是存储一个

字符指针(通常是4)所需的内存量,而不是像你似乎所假设的那样,内存的数量是什么''
'' curtime''指向(你不能单凭

a指针)。所以制作


snprintf(curtime,strlen(p)+ 3,"%c%s%c",''['',p,'']'') ;


或者,或许更简单,


snprintf(curtime,strlen(p)+ 3," [%s]" ,p'');


(由于字符串末尾的''\0'',使用3而不是2,否则

最后的'''''不会出现在字符串中。)


但你不需要snprintf(),sprintf()会做,因为你知道

提前打印字符串需要多长时间(如果你的计算结果正确,最少
;-)


sprintf(curtime," [%s]",p);

printf("%s \ n",curtime);
返回curtime;
}



问候,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de
\ __________________________ http://www.toerring.de


I am having a problem with the following functions, I''ve been racking my
brains trying to figure out where I am going wrong. What I need to do is
return a formatted string with the current date and time in the format of:

[Thu May 5 17:06:27 2005]

Here is my function as it stands:

char *currenttime(){
char *curtime;
time_t t;
char *p, *e;
t = time(NULL);
p = ctime(&t);
if (p && ((e = strchr(p, ''\n'')))) *e = ''\0'';
curtime = malloc(strlen(p)+2);
if(curtime == NULL) {
printf("out of memory\n");
exit(1);
}
snprintf(curtime, sizeof(curtime+2), "%c%s%c", ''['', p, '']'');
printf("%s\n", curtime);
return curtime;
}

I hope someone can help me.
--
Materialised
perl -e ''printf %silto%c%sal%c%s%ccodegurus%corg%c, ma, 58, mw, 107,
''er'', 64, 46, 10;''
Bart: "What''s Santa''s Little Helper doing to that dog? Looks like he''s
trying to jump over, but he can''t quite make it."

解决方案

Materialised wrote:

I am having a problem with the following functions, I''ve been racking my brains trying to figure out where I am going wrong. What I need to do is return a formatted string with the current date and time in the format of:
[Thu May 5 17:06:27 2005]

Here is my function as it stands:

char *currenttime(){
char *curtime;
time_t t;
char *p, *e;
t = time(NULL);
p = ctime(&t);
if (p && ((e = strchr(p, ''\n'')))) *e = ''\0'';
curtime = malloc(strlen(p)+2);
if(curtime == NULL) {
printf("out of memory\n");
exit(1);
}
snprintf(curtime, sizeof(curtime+2), "%c%s%c", ''['', p, '']'');
printf("%s\n", curtime);
return curtime;
}



sizeof(curtime+2) is odd usage. It mean the same as sizeof(curtime) I
guess, since the type of curtime+2 is the same as the type of curtime?
Anyway, it yields the number of bytes a pointer to char occupies on
your system, often 4 bytes of 32 bit systems, but not necessarily that.

You want something more like this:
size_t len = strlen(p)+3; /* allow 1 for each char, 1 for ''\0'' */
curtime = malloc(len);
....
snprintf(curtime, len, "%c%s%c", ''['', p, '']'');

-David


Materialised wrote:

I am having a problem with the following functions, I''ve been racking my
brains trying to figure out where I am going wrong. What I need to do is
return a formatted string with the current date and time in the format of:

[Thu May 5 17:06:27 2005]

Here is my function as it stands:

char *currenttime(){
char *curtime;
time_t t;
char *p, *e;
t = time(NULL);
p = ctime(&t);
if (p && ((e = strchr(p, ''\n'')))) *e = ''\0'';
curtime = malloc(strlen(p)+2);
if(curtime == NULL) {
printf("out of memory\n");
exit(1);
}
snprintf(curtime, sizeof(curtime+2), "%c%s%c", ''['', p, '']''); Your problem: sizeof(curtime + 2)
Explanation:
curtime is a pointer.
sizeof(curtime) is the size of the _pointer_.
You want (strlen(p) + 2) instead.

printf("%s\n", curtime);
return curtime;
}

I hope someone can help me.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


In comp.lang.c Materialised <Ma**********@privacy.net> wrote:

I am having a problem with the following functions, I''ve been racking my
brains trying to figure out where I am going wrong. What I need to do is
return a formatted string with the current date and time in the format of: [Thu May 5 17:06:27 2005] Here is my function as it stands: char *currenttime(){
char *curtime;
time_t t;
char *p, *e;
t = time(NULL);
p = ctime(&t);
if (p && ((e = strchr(p, ''\n'')))) *e = ''\0'';
First problem: ''e'' is an uninitialized pointer, so you can''t store
anything where it''s currently pointing to. But since ''e'' is never
going to be used you can simply remove the whole line.
curtime = malloc(strlen(p)+2);
Since you are going to store a string in ''curtime'' you need one
more char for the trailing ''\0''. You have to use

curtime = malloc( strlen( p ) + 3 );
if(curtime == NULL) {
printf("out of memory\n");
exit(1);
}
snprintf(curtime, sizeof(curtime+2), "%c%s%c", ''['', p, '']'');
''curtime'' is a char pointer, so ''sizeof(curtime)'' as well as
''sizeof(curtime+2)'' is the amount of memory needed for storing a
char pointer (often 4), not, as you seem to assume, the amount of
memory of what ''curtime'' points to (you can''t figure that out from
a pointer alone). So make that

snprintf( curtime, strlen( p ) + 3, "%c%s%c", ''['', p, '']'');

or, perhaps more simple,

snprintf( curtime, strlen( p ) + 3, "[%s]", p'');

(use 3 instead of 2 because of the ''\0'' at the end of a string, otherwise
the final '']'' won''t appear in the string).

But you don''t need snprintf() here, sprintf() will do since you know
in advance how long the string is going to be you''re printing (at
least if you get the calculations right;-)

sprintf( curtime, "[%s]", p );
printf("%s\n", curtime);
return curtime;
}


Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


这篇关于以下功能有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆