I am having a problem with the following functions, I''ve been racking mybrains trying to figure out where I am going wrong. What I need to do isreturn 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.--Materialisedperl -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''strying to jump over, but he can''t quite make it." 解决方案 Materialised wrote: I am having a problem with the following functions, I''ve been rackingmy brains trying to figure out where I am going wrong. What I need to dois return a formatted string with the current date and time in theformat 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) Iguess, 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 onyour 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 MatthewsC++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txtC++ Faq: http://www.parashift.com/c++-faq-liteC Faq: http://www.eskimo.com/~scs/c-faq/top.htmlalt.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 LibraryIn 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 storeanything where it''s currently pointing to. But since ''e'' is nevergoing 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 onemore char for the trailing ''\0''. You have to usecurtime = 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 achar pointer (often 4), not, as you seem to assume, the amount ofmemory of what ''curtime'' points to (you can''t figure that out froma pointer alone). So make thatsnprintf( 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, otherwisethe final '']'' won''t appear in the string).But you don''t need snprintf() here, sprintf() will do since you knowin advance how long the string is going to be you''re printing (atleast 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 这篇关于以下功能有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 18:30
查看更多