A function that formats data to a string - sprintf_s

Function prototype: sprintf_s(char * const _Buffer, size_t const _BufferCount, char const * const _Format, …)
where:
const _Buffer is a char-type array used to store strings, that is, the buffer area;
const _BufferCount is the length of the buffer, which The size cannot exceed the length of the defined const _Buffer;
const _Format is a format string, and the form and usage here are consistent with the printf function.
eg:

void test()
{
	SYSTEMTIME nowtime;
	GetLocalTime(&nowtime);
	
	char m_fileName[200];
	strcpy_s(m_fileName, "BIT");
	
	sprintf_s(&m_fileName[strlen(m_fileName)], 200- strlen(m_fileName), "_%04d%02d%02d%02d%02d%02d.log", nowtime.wYear, nowtime.wMonth, nowtime.wDay, nowtime.wHour, nowtime.wMinute, nowtime.wSecond);
	cout << m_fileName << endl;
	
	fstream m_file;
	m_file.open(m_fileName, ios::out | ios::app);
	
	GetLocalTime(&nowtime);
	sprintf_s(m_fileName, 200, "%02d:%02d:%02d.%03d  Log closed.", nowtime.wHour, nowtime.wMinute, nowtime.wSecond, nowtime.wMilliseconds);
	
	m_file << m_fileName << endl << endl;
	m_file.close();
}

Note: const _Buffer is best defined in the form of an array, so that its size can be clearly known, not in the form of a pointer, so as to avoid unnecessary problems.

Guess you like

Origin blog.csdn.net/weixin_44941350/article/details/123552004