When I use ctime and time, it always breaks a line after it outputs... How do I change this behaviour so that it doesn't break line?
When I use ctime and time, it always breaks a line after it outputs... How do I change this behaviour so that it doesn't break line?
Please post code so we can see what the problem really is.
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
After the ctime, the line breaks...Code:if (exp == "y") { time(&timz); ofstream exp("export.txt", ios::app); exp<<ctime(&timz)<<"Integer Benchmark: "<<y<<" seconds."; exp.close(); }
Here's your answer:
"Code:char * ctime ( const time_t * timer );
Convert time_t value to string.
Converts timer to a string containing time and date adjusted to local time zone in readable format.
timer is an integer value of type time_t defined as long int by default in most compilers, and usually returned by a call to time function.
The returned string has the following format:
Www Mmm dd hh:mm:ss yyyy
where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year. The string is followed by a new-line character (\n) and a terminating null-character, conforming a total of 26 characters."
- Micko
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
So there is no way to get around this?
if you want to display in one line use something like this:
- MickoCode:char* tmp = ctime(....); char buf[30]; strcpy(buf, tmp); char* p = strchr(buf,'\n'); if (p) { *p = '\0'; }
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
Micko this is c++ no need for that ugly
Code:std::string thetime = ctime(&timz); thetime.erase(thetime.find('\n', 0), 1); exp<<thetime<<" Integer Benchmark: "<<y<<" seconds.";
It's really nice and elegant, thank you Thantos!
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
Thank you guys.. it works!
I think the C version looks betterOriginally Posted by Thantos
But why the inconsistent use of std namespace?
because they had already provided the rest of the code save the string. It's called copy and paste
You could always use strftime() which is the general purpose routine, of which ctime() is but a special case.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
> Now where is the fun in that Salem?
Passing it NULL
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.