SyntaxError
SyntaxError

Reputation: 1752

char[] to CString Conversion

I have time in char[] format but I need to convert it to CString. Here is what I have but it does not work :

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;

Any help..:( ?

Upvotes: 0

Views: 10420

Answers (2)

james raygan
james raygan

Reputation: 701

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, ...);

UPDATE : ANSWER TO YOU FIRST COMMENT :

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

Upvotes: 1

praks411
praks411

Reputation: 1992

You can use std::ostringstream and std::string to convert time into string. Something like this. I've shown for seconds similarly you can do for hours, minutes etc.

int seconds;
std::ostringstream sec_strm;
sec_strm << seconds;
std::string sec_str(sec_strm.c_str());

Upvotes: 0

Related Questions