Localtime_s and strcpy_s

Please advise how to make errors and warnings in the following three situations go away. Thank you in advance.


Situation 1

char buffer[90];
void get_time(char* buffer, int size)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime_s( &timeinfo, &rawtime ); (L175)
strftime (buffer, size, "%I:%M:%S %p",timeinfo);
}

Warnings
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(175): warning C4047: 'function': 'tm *const ' differs in levels of indirection from 'tm **'
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(175): warning C4024: 'localtime_s': different types for formal and actual parameter 1
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(175): warning C4047: '=': 'tm *' differs in levels of indirection from 'errno_t'


Situation 2

char buffer[90];
void write_time(char* s)
{
strcpy_s( buffer, s); (L180)
get_time(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer));
write_msg(buffer, 0 );
}

Warnings and error
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(180): warning C4047: 'function': 'std::rsize_t' differs in levels of indirection from 'char *'
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(180): warning C4024: 'strcpy_s': different types for formal and actual parameter 2
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(180): error C2198: 'strcpy_s': too few arguments for call


Situation 3

strcpy_s(strrchr(filnam, '.'), ".bin"); (L270)

Warnings and error
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(270): warning C4047: 'function': 'std::rsize_t' differs in levels of indirection from 'char [5]'
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(270): warning C4024: 'strcpy_s': different types for formal and actual parameter 2
1>..\Venext6\External Sub Functions\hospital\Hsglob.c(270): error C2198: 'strcpy_s': too few arguments for call
Situation 1:

If you look at the documentation for localtime_s, you'll see that the first argument sould be a time_t *, and the second should be a tm * :

https://en.cppreference.com/w/c/chrono/localtime

You seem to have your first and second arguments reversed.

Also: You declare timeinfo to be a tm*, so obviously &timeinfo is a tm**. Obviously, you need to simply pass in a tm* - presumably, just timeinfo.

Situation 2:

If you look at the documentation for strcpy_s, you'll see that the second argument should be an rsize_t, not a char*. To fix it, pass the correct arguments in.

Situation 3:

Same as situation 2.

For all 3 situations:

When your compiler is telling you that you're not passing the right types of arguments into standard library functions, you should look at the documentation for those functions, to understand how they work, and what arguments to pass in.
Last edited on
for 1) and 2) consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;

char buffer[90] {};

void get_time(char* buff, int size)
{
	time_t rawtime {};
	tm timeinfo {};

	time(&rawtime);
	localtime_s(&timeinfo, &rawtime);
	strftime(buff, size, "%I:%M:%S %p", &timeinfo);
}

void write_time(const char* s)
{
	const auto sl {strlen(s)};

	strcpy_s(buffer, s);
	get_time(buffer + sl, sizeof(buffer) - sl);
	//write_msg(buffer, 0);
	cout << buffer << '\n';
}

int main()
{
	write_time("time: ");
}


For 3)

 
	strcpy_s(strrchr(filnam, '.'), 5, ".bin");

For the Microsoft implementation of the library (Visual Studio), which is the one being used,
get_time() should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <time.h>

// using the Microsoft library 
void get_time( char* buffer, size_t size )
{
    time_t rawtime;
    // struct tm* timeinfo;
    struct tm timeinfo ; // ****** we need to provide the tm object
    
    time( &rawtime );
    
    // timeinfo* = localtime_s( &timeinfo, &rawtime ); // (L175)
    // "The implementation of localtime_s in Microsoft CRT is incompatible with the C standard 
    // since it has reversed parameter order and returns errno_t." - cppreference
    // MSDN: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/localtime-s-localtime32-s-localtime64-s?view=msvc-160
    const errno_t result = localtime_s( &timeinfo, &rawtime ) ;

    if( result == 0 ) strftime( buffer, size, "%I:%M:%S %p", &timeinfo );
    else buffer[0] = 0 ;
}
Topic archived. No new replies allowed.