Stop ctime from breaking line?

This is a discussion on Stop ctime from breaking line? within the C++ Programming forums, part of the General Programming Boards category; When I use ctime and time, it always breaks a line after it outputs... How do I change this behaviour ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    43

    Stop ctime from breaking 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?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    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

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Code:
                           if (exp == "y")
                           {
                                        time(&timz);
                                        ofstream exp("export.txt", ios::app);
                                        exp<<ctime(&timz)<<"Integer Benchmark: "<<y<<" seconds.";
                                        exp.close();
                           }
    After the ctime, the line breaks...

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    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

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    So there is no way to get around this?

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    if you want to display in one line use something like this:
    Code:
    char* tmp = ctime(....);
    char buf[30];
    
    strcpy(buf, tmp);
    
    char* p = strchr(buf,'\n');
    
    if (p)
    {
        *p = '\0';
    }
    - 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

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.";

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    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

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Thank you guys.. it works!

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Thantos
    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.";
    I think the C version looks better

    But why the inconsistent use of std namespace?

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    because they had already provided the rest of the code save the string. It's called copy and paste

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Now where is the fun in that Salem?

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 01:41 AM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21