Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hello guys.... I have time in char[] format but I need to convert it to CString. Here is what I have but it does not work

C++
GetSystemTime(&t);
char time[60] = "";
char y[20],mon[20],d[20],h[20],min[20],s[20];

sprintf(y, "%d", t.wYear);
sprintf(d, "%d", t.wDay);
sprintf(mon, "%d", t.wMonth);
sprintf(h, "%d", t.wHour+5);
sprintf(min, "%d", t.wMinute);
sprintf(s, "%d", t.wSecond);

strcat(time,d);
strcat(time,"/");
strcat(time, mon);
strcat(time,"/");
strcat(time, y);
strcat(time," ");
strcat(time,h);
strcat(time,":");
strcat(time, min);
strcat(time,":");
strcat(time, s);

CString m_strFileName = time;
Posted
Comments
wizardzz 4-Mar-11 16:43pm    
Seriously, have you tried google yet?
Sergey Alexandrovich Kryukov 4-Mar-11 17:11pm    
Wow!
--SA
AmbiguousName 5-Mar-11 8:00am    
@wizardzz....yup, for almost 3 and a half hours I googled this.
mbue 5-Mar-11 8:52am    
you can't use colon within a file name! Thats all, see:
http://msdn.microsoft.com/en-us/library/aa368590%28VS.85%29.aspx
Regards.

Try this :

CString m_strFileName = new CString(time);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-11 17:12pm    
Well, a 5.
--SA
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); //This uses the CString::CString(const char *) constructor
//Note: If m_strFileName is a member variable of a class (as the m_ suggests), then you should use the = operator and not the variable declaration like this:
m_strFileName = time; //This variable is already defined in the class definition


2: Using CString::Format[^]:
CString m_strFileName; //Note: This is only needed if m_strFileName is not a member variable of a class
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");
//or...
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); //The cast operator here gets a read-only value of the string
 
Share this answer
 
v2
Comments
AmbiguousName 5-Mar-11 8:38am    
Thank You very much. This was reeeeeeaaaaaaaaally helpful.
Be careful of the character set.
Starting with VS 2008 (or possibly 2005 which I didn't use much) Unicode is the default characters set for new projects.
If this is the case, the CString will be in Unicode and time will be in ASCII. Character conversions are not done by default.
If this is the case, either:
Change this in the General Project Properties to "Not Set" (easier), or
Change your code to #include <tchar.h> and change things like strcat to _tcscat. Check MSDN or google for "visual studio tchar" for more details if you cant understand it. (better)

Also, a note on performance. What is wrong with just using this
char time[60]; //Count the characters used. Max is 20 including the '\0' if the values are valid.
sprintf(time, "%u/%u/%u %u:%u:%u", t.wDay, t.wMonth, t.wYear, t.wHour + 5, t.wMinute, t.wSecond);

It does the same as your code, but uses less memory, executes faster and takes less lines of code.

This is better because:
char time[60] = ""; will allocate 60 bytes of space in the .exe file whereas char time[60] will only push 60 bytes onto the stack when required without allocating space in the .exe. Slightly smaller file size.
You are using less buffers, which requires less space on the stack.
You are copying less memory around without the strcat.
%u is unsigned, %d is signed, unless you want negative dates or times a good convention is to use unsigned numbers when printing.
 
Share this answer
 
Comments
AmbiguousName 5-Mar-11 8:04am    
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. Thats why I am looking for this solution. 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900