overloaded Name wrote:
That did not work for me. Actually I am recording my voice using MIC and I want to save this audio file with the name of current date.
If you have a file extension, then the best place to put it would be in the sprintf/CString::Format call while formatting the date string.
Also, generally when formatting a date for a file name, it is done in reverse order yyyy/mm/dd so that sorting works correctly in the Windows Explorer.
1 Final thing before I jump into some code: there are invalid characters for file names in Windows, among these are both the slash characters [EDIT] and the colon character[/EDIT]. Generally dots or dashes are used instead for file names.
My solutions use the slashes and date format you use, keeping with your code, but you should change at least the slashes if you are using it for file names.
Let me provide a few solutions for you:
1: Similar to what you had:
char time[60];
sprintf(time, "%u/%u/%u %u:%u:%u", t.wDay, t.wMonth, t.wYear, t.wHour + 5, t.wMinute, t.wSecond);
CString m_strFileName(time);
m_strFileName = time;
2: Using
CString::Format[
^]:
CString m_strFileName;
m_strFileName.Format("%u/%u/%u %u:%u:%u", t.wDay, t.wMonth, t.wYear, t.wHour + 5, t.wMinute, t.wSecond);
3: Why are you using a CString?
If it is not a member variable of a class, then you don't need to use a CString you can use
time
directly.
char time[60];
sprintf(time, "%u/%u/%u %u:%u:%u", t.wDay, t.wMonth, t.wYear, t.wHour + 5, t.wMinute, t.wSecond);
FILE *pFile = fopen(time, "w");
HANDLE hFile = CreateFile(time, ...);
overloaded Name wrote:
Now if I can convert this into CString, then I"ll use CString's GetBuffer() method to convert it into LPSTR. Unfortunately im very poor in these sort of conversions.
No.
CString::GetBuffer[
^] is used for getting a mutable buffer of the CString that you can write to, generally as the buffer for sprintf, GetModuleFilename, ... functions.
If you just want the value of the string for reading, use the casting operator like this:
CString str("hello");
printf("%s\n", (LPCSTR)str);