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?
Printable View
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.
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
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';
}
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!
Thank you guys.. it works!
I think the C version looks better ;)Quote:
Originally 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.
Now where is the fun in that Salem?
> Now where is the fun in that Salem?
Passing it NULL :p