Using CTime & asctime to assign time to a string or vector string
Question
I would like to use asctime to assign the time to a string.
time_t rawtime;
time ( &rawtime );
vector<string> TTime;
TTime.resize(10);
TTime = asctime(localtime ( &rawtime ));
I understand asctime is returning a pointer to a string. Would i have to create my own string and assign it the return value of asctime, or is there a simpler way?
Solution
You can construct a string directly from a char *
:
string str = asctime(localtime ( &rawtime ));
This doesn't make sense:
TTime = asctime(localtime ( &rawtime ));
You can't assign a single string to a vector of strings. What you can do is:
TTime[0] = asctime(localtime ( &rawtime ));
OTHER TIPS
Looks like what you need is a simple string,
std::string TTime(asctime(localtime(&rawtime)));
The function asctime() return char* and the std::string can construct from char*
std::string time(asctime(localtime(&rawtime)));
or
std::string time; time = asctime(asctimer(localtimer(&rawtime)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow