nagkiller
Full Member level 4
Could anyone help me with a error in char function on Visual Studio C 2019.
Inside function, result is ok, but in main function it is printing differents characters.
If I change main function to use strncpy_s, a half of result is printed.
The result is:
Thanks in advance.
C++:
#include <windows.h>
#include <iostream>
char *timeStamp();
int main(void) {
printf_s("%s", timeStamp());
return 0;
}
char *timeStamp() {
SYSTEMTIME t;
GetLocalTime(&t);
char buffer[35];
int j;
j = sprintf_s(buffer, 25, "%02d/", t.wDay);
j += sprintf_s(buffer + j, 30 - j, "%02d/", t.wMonth);
j += sprintf_s(buffer + j, 30 - j, "%04d ", t.wYear);
j += sprintf_s(buffer + j, 30 - j, "%02d:", t.wHour);
j += sprintf_s(buffer + j, 30 - j, "%02d:", t.wMinute);
j += sprintf_s(buffer + j, 30 - j, "%02d = ", t.wSecond);
printf_s("%s\n", buffer);
return buffer;
}
Inside function, result is ok, but in main function it is printing differents characters.
Code:
Inside function ===> 10/11/2020 22:07:11 =
Outside function ==> ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠♀■´
If I change main function to use strncpy_s, a half of result is printed.
C++:
int main(void) {
char timeNow[35];
strncpy_s(timeNow,35, timeStamp(), 34);
printf_s("%s\n", timeNow);
return 0;
}
The result is:
Code:
10/11/20☺
Thanks in advance.
Last edited: