Need help with an error message while using asctime_s

Hi,

I am trying to create a timestamp that outputs the current time to the user.

When I run my program currently I am getting the following error. Please see image below.
http://i.imgur.com/yuVaWKe.png


1
2
3
4
5
char buffer[256];
time_t rawtime;
tm timeinfo;
errno_t result = localtime_s(&timeinfo, &rawtime);
cout << "Current local time and date: " << asctime_s(buffer, &timeinfo) << endl;


Can anyone please advise?

Kind Regards
Last edited on
My first suggestion is that you study the documentation for the "safe" function and use it correctly. Pay attention to the number and types of the arguments and the return value. This function doesn't return a pointer to a tm instance.

Jim
I have changed the code to

1
2
3
4
5
char buffer[256];
time_t rawtime = (0);
tm timeinfo;
errno_t result = localtime_s(&timeinfo, &rawtime);
cout << "Current local time and date: " << asctime_s(buffer, &timeinfo) << endl;


However it always outputs to 0. Any ideas how I can get it to output the actual date and time.
Now it looks like you also need to read the documentation for asctime_s() and use the correct number and types of parameters and pay attention to the value that this function returns.



I've read the documentation here - http://msdn.microsoft.com/en-us/library/b6htak9c.aspx
But I am struggling to understand what it returns. Is there any other documentation?
I really don't understand what you don't understand (from the page you provided):


Return Value

Zero if successful. If there is a failure, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, the return value is an error code. Error codes are defined in ERRNO.H. For more information, see errno Constants. The actual error codes returned for each error condition are shown in the following table.


Do you realize that this is a non-standard compiler specific function*? The C++ standard doesn't support these "safe" functions.

You need to print the first parameter, not the return value. The return value only tells you if the function succeeded.


* Although the C11 standard specifies these functions as optional, C++ doesn't yet support these "safe" functions. And by the way they are only "safe" when you use them properly, which you're still not doing. This function takes three arguments, not two.

Perhaps you should consider using the standard C functions localtime() and asctime() instead.



Topic archived. No new replies allowed.