943,895 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4362
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 22nd, 2009
0

Help with asctime_s and localtime_s

Expand Post »
Hi. I'm making a class in an include file. I ran into some touble...When i try to compile, this is what comes up:

C++ Syntax (Toggle Plain Text)
  1. c:\...\renew.h(25) : error C2660: 'localtime_s' : function does not take 1 arguments
  2. c:\...\renew.h(36) : error C2660: 'asctime_s' : function does not take 1 arguments

Here is the code for the class ([#include time.h is in the main cpp file)

C++ Syntax (Toggle Plain Text)
  1. class renewBooks
  2. {
  3. public:
  4. int getItems()
  5. {
  6. cout << "Please scan the barcode of the book you would like to renew.";
  7. cin >> renewBarcode;
  8. processItems(renewBarcode);
  9. return false;
  10. }
  11.  
  12.  
  13. private:
  14. string renewBarcode;
  15. string toRenewItem;
  16.  
  17. int processItems(string toRenewItem)
  18. {
  19. char date[9];
  20. _strdate_s(date);
  21. char time2 [9];
  22. _strtime_s(time2);
  23.  
  24. time_t now = time(NULL);
  25. struct tm* tm = localtime_s(&now);
  26. tm->tm_mday +=14;
  27. // asctime(tm);
  28.  
  29. ofstream renew ("renewBooks.txt", std::ios::app);
  30.  
  31. renew << "Date: "
  32. << date
  33. << "\nTime: "
  34. << time2
  35. << "\nDue date: "
  36. << asctime_s(tm)
  37. << "\nBook barcode: "
  38. << toRenewItem;
  39.  
  40. renew.close();
  41. return false;
  42. }
  43. };
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Feb 22nd, 2009
0

Re: Help with asctime_s and localtime_s

those error messages are correct. Did you look up those functions in msdn to see what are their parameters? If not, then you should do so. You have to be careful about those non-standard Microsoft functions. Microsoft declared many standard C functions depreciated, the c and c++ standards made no such statement. If you want to use the _s functions you need to also understand that other compilers don't support them. So if you turn in that assignment to your teacher and he/she doesn't use the same compiler then your program won't compile and you may get a lower grade.
Last edited by Ancient Dragon; Feb 22nd, 2009 at 10:23 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Feb 22nd, 2009
0

Re: Help with asctime_s and localtime_s

Hi. First, I program for the fun of it and to make things of use to me. Second, I did look up the function in the time.h file and all i saw was...well...gibberish. I tried adding another argument (tm, false) to the code, but that just generated more errors. Please help.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Feb 22nd, 2009
0

Re: Help with asctime_s and localtime_s

you won't find those functions in time.h because they are non-standard functions, as I previously said. You have to look them up at the Microsoft web site. Just use google and you will find them (just click that link).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Feb 23rd, 2009
0

Re: Help with asctime_s and localtime_s

Ok... so, I looked it up on themicrosoft's msdn website and this is what i wrote, but i still got ERREORS !
Here is the code and the errors

C++ Syntax (Toggle Plain Text)
  1. class renewBooks
  2. {
  3. public:
  4. int getItems()
  5. {
  6. cout << "Please scan the barcode of the book you would like to renew:\n";
  7. cin >> renewBarcode;
  8. processItems(renewBarcode);
  9. return false;
  10. }
  11.  
  12.  
  13. private:
  14. string renewBarcode;
  15. string toRenewItem;
  16.  
  17. #include <time.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. int processItems(string toRenewItem)
  21. {
  22. struct tm newtime;
  23. char am_pm[] = "AM";
  24. _time64_t long_time;
  25. char timebuff [26];
  26. errno_t err;
  27.  
  28. //Get time as a 64 bit integer
  29. _time64( &long_time );
  30. // Convert to local time
  31. err = _localtime64_s( &newtime, &long_time );
  32. if (err)
  33. {
  34. printf( "Invalid argument to _local time64_s.");
  35. exit(1);
  36. }
  37.  
  38. if(newtime.tm_hour > 12)
  39. strcpy_s(am_pm, sizeof(am_pm), "PM");
  40. if (newtime.tm_hour > 12)
  41. newtime.tm_hour -= 12;
  42. if (newtime.tm_hour == 0)
  43. newtime.tm_hour = 12;
  44.  
  45. // Convert to an ASCII representation
  46. err = asctime_s(timebuff, 26, &newtime);
  47. if (err)
  48. {
  49. printf ( "Invalid argument to asctime_s.");
  50. exit(1);
  51. }
  52.  
  53. ofstream renew ("renewBooks.txt", std::ios::app);
  54.  
  55. renew << "Date: "
  56. << "\nTime: "
  57. << "\nDue date: ";
  58. renew printf( "%.19s %s\n", timebuff, am_pm );
  59. renew << "\nBook barcode: "
  60. << toRenewItem;
  61.  
  62. renew.close();
  63. return false;
  64. }
  65. };
  66.  
  67.  
  68.  
  69.  
  70. ------ Build started: Project: Lib. Application Framework, Configuration: Debug Win32 ------
  71. Compiling...
  72. mainSystemFramework.cpp
  73. c:\...\renew.h(24) : error C2065: '_time64_t' : undeclared identifier
  74. c:\...\renew.h(24) : error C2146: syntax error : missing ';' before identifier 'long_time'
  75. c:\...\renew.h(24) : error C2065: 'long_time' : undeclared identifier
  76. c:\...\renew.h(29) : error C2065: 'long_time' : undeclared identifier
  77. c:\...\renew.h(31) : error C2065: 'long_time' : undeclared identifier
  78. c:\...\renew.h(58) : error C2146: syntax error : missing ';' before identifier 'printf'
  79. Lib. Application Framework - 6 error(s), 0 warning(s)
  80. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Feb 24th, 2009
0

Re: Help with asctime_s and localtime_s

This secure C library stuff is in unstable state now so better don't waste a time with these *_s functions. They have the same functionality as old good standard functions.

You will have troubles with 32-bit time values after 2038 year only so better forgive that _time64 and time_64_t names. Don't worry, year 2038 won't turn up for a while yet ...

If you need platform-independend codes, define some obvious macros to convert *_s and time64 names to standard names in Linux environment - it's well-known and productive approach...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Feb 24th, 2009
0

Re: Help with asctime_s and localtime_s

I don't know about other compilers but Microsoft has already converted standard time_t to a 64-bit number unless you specifically ask for a 32-bit number
C++ Syntax (Toggle Plain Text)
  1. //
  2. // extracted from time.h
  3. //
  4. #ifndef _TIME_T_DEFINED
  5. #ifdef _USE_32BIT_TIME_T
  6. typedef __time32_t time_t; /* time value */
  7. #else
  8. typedef __time64_t time_t; /* time value */
  9. #endif
  10. #define _TIME_T_DEFINED /* avoid multiple def's of time_t */
  11. #endif

So using _time64_t isn't necessary. Just use standard time_t and your program will be find.
Last edited by Ancient Dragon; Feb 24th, 2009 at 9:27 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Feb 24th, 2009
0

Re: Help with asctime_s and localtime_s

Thanks guys, you great. Now I have one more question... Is there any way to physically open the text file for the user to print and save as they wish after it has been wirtten to? If there is a way, that would be great. Thank you!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009
Feb 24th, 2009
0

Re: Help with asctime_s and localtime_s

On Windows try this (with #include <windows.h>, of course):
c++ Syntax (Toggle Plain Text)
  1. void editFile(const char* filepath)
  2. {
  3. if (filepath) {
  4. HWND wnd = ::GetDesktopWindow();
  5. ::ShellExecute(wnd,"open",filepath,0,0,0);
  6. }
  7. }
Let a user plays with his/her preferred text editor...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Feb 25th, 2009
0

Re: Help with asctime_s and localtime_s

Ot, then, i tried to add days to my date by putting in:

C++ Syntax (Toggle Plain Text)
  1. newtime.tm_mday += 14;

and then, I got a huge runtime error. I took it out, and it was fine. Is there a better way?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
cppnewb is offline Offline
52 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: peincrement and postincrement operator
Next Thread in C++ Forum Timeline: #include "backward_warning.h"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC