Thread: linux ctime(date as a name)

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    4

    linux ctime(date as a name)

    Hey guys,

    i try to create a file : fopen(test,"w+");
    the filename is to be the current date, i tried to realize this like:

    time_t t;
    time(&t);
    char *test = ctime(&t);
    ctime returns a value with 26 letters like "Sat Feb 17 19:31:26 1996\n\0".
    And now i try to delete the \n\0 because of this the filename of the created file is the date with a "?" at the end but i dont know how i can fix this.

    regards
    septix

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Replace the \n with \0 if you don't want it in your filename.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Replace the \n with \0 if you don't want it in your filename.
    but how can i do that?

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Look at the example code here. Scroll down and you will see the code snippet for testing for and removing the new line character. This can be applied for any character.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    How did you "delete" the \n\0. If you somehow "remove" the \0, you no longer have a valid string, since a string in C must end with a \0. All you really need to do is find the \n and replace it with a \0, anything after that will be ignored.

    Also, you can look into the strftime function that will allow you to make any format you want from the current time.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    4
    Quote Originally Posted by anduril462 View Post
    How did you "delete" the \n\0. If you somehow "remove" the \0, you no longer have a valid string, since a string in C must end with a \0. All you really need to do is find the \n and replace it with a \0, anything after that will be ignored.

    Also, you can look into the strftime function that will allow you to make any format you want from the current time.
    Ahh the strftime function did it!

    Thank you guys very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with ctime
    By Programmer_P in forum C++ Programming
    Replies: 24
    Last Post: 03-03-2011, 07:56 PM
  2. need help with ctime
    By volk in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2003, 08:02 PM
  3. Preserving Windoze date while installing linux
    By windoze victim in forum Tech Board
    Replies: 10
    Last Post: 04-02-2003, 10:17 PM
  4. ctime - current date
    By razrektah in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2001, 12:42 PM
  5. how to pull system time, date, etc from <ctime>
    By Matt in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2001, 09:08 AM

Tags for this Thread