Getting the date and write a file with it in it.

This is a discussion on Getting the date and write a file with it in it. within the C++ Programming forums, part of the General Programming Boards category; Hello Ya'll, I've been having a bit of trouble trying to get the system's date. And then saving a file ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Question Getting the date and write a file with it in it.

    Hello Ya'll, I've been having a bit of trouble trying to get the system's date. And then saving a file with the date in it. For example today is 31-10-06. It would be cool if I could save the file as CKVproject311006.htm (or CKVproject31102006.htm). So that everyday I make a new file it will save it towards the date.

    Thanks for reading and I hope someone can help me =)

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    I think you need to include the Windows.h header for this (assuming you're using Windows...)

    Code:
    SYSTEMTIME s;	
    GetSystemTime(&s);
    then you could do something along the lines of
    Code:
    char* fname;
    sprintf(fname, "CKVproject%d%d%d.htm", s.wDay, s.wMonth, s.wYear);
    I believe that should do what you want...not sure if the syntax is precise though.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    Code:
    sprintf(fname, "CKVproject%d%d%d.htm", s.wDay, s.wMonth, s.wYear);
    better use format "%02d%02d%04d"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > char* fname;
    > sprintf(fname, "CKVproject%d%d%d.htm", s.wDay, s.wMonth, s.wYear);
    You also need to make sure the thing you're trying to print into is actually pointing to allocated space.
    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.

  5. #5
    Registered User Xeridanus's Avatar
    Join Date
    Oct 2006
    Location
    QLD, Aussieland
    Posts
    11
    what about the portable time.h library? in that is a function that does this kind of stuff, strftime() i believe. hang on i go get the prototype.
    Code:
    size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr);
    i know that is the C version, but i don't know the C++ equivilant. i just realised you would have to know the format codes as well.

    %d == day of the month, 01-31
    %m == month of the year, 01-12
    %y == year without century, 00-99
    NB: they are case sensative. any unknown characters are put straight into the string as is.
    also NB: use localtime(time(NULL)) to get the "struct tm *timeptr"

    hope that helped

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Sorry to say guys, but these codes don't work >.<

    It will simply exit without an error when I want to write a file...

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    have you checked that the file name created is actually what you planned to build? before trying to open the file with such a name?
    have you tried to create the file with such name from the command prompt?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Of course posting "it doesn't work" without posting any code at all REALLY helps us to figure out what problem you're facing
    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.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    But we can just answer:
    "I compiled it and it worked!"
    also without posting any code. Easy, hah?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 11:31 AM
  5. File I/O with a current Date
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2002, 10:56 PM

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