| | |
Help with asctime_s and localtime_s
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
Here is the code for the class ([#include time.h is in the main cpp file)
C++ Syntax (Toggle Plain Text)
c:\...\renew.h(25) : error C2660: 'localtime_s' : function does not take 1 arguments 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)
class renewBooks { public: int getItems() { cout << "Please scan the barcode of the book you would like to renew."; cin >> renewBarcode; processItems(renewBarcode); return false; } private: string renewBarcode; string toRenewItem; int processItems(string toRenewItem) { char date[9]; _strdate_s(date); char time2 [9]; _strtime_s(time2); time_t now = time(NULL); struct tm* tm = localtime_s(&now); tm->tm_mday +=14; // asctime(tm); ofstream renew ("renewBooks.txt", std::ios::app); renew << "Date: " << date << "\nTime: " << time2 << "\nDue date: " << asctime_s(tm) << "\nBook barcode: " << toRenewItem; renew.close(); return false; } };
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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).
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
Here is the code and the errors
C++ Syntax (Toggle Plain Text)
class renewBooks { public: int getItems() { cout << "Please scan the barcode of the book you would like to renew:\n"; cin >> renewBarcode; processItems(renewBarcode); return false; } private: string renewBarcode; string toRenewItem; #include <time.h> #include <stdio.h> #include <string.h> int processItems(string toRenewItem) { struct tm newtime; char am_pm[] = "AM"; _time64_t long_time; char timebuff [26]; errno_t err; //Get time as a 64 bit integer _time64( &long_time ); // Convert to local time err = _localtime64_s( &newtime, &long_time ); if (err) { printf( "Invalid argument to _local time64_s."); exit(1); } if(newtime.tm_hour > 12) strcpy_s(am_pm, sizeof(am_pm), "PM"); if (newtime.tm_hour > 12) newtime.tm_hour -= 12; if (newtime.tm_hour == 0) newtime.tm_hour = 12; // Convert to an ASCII representation err = asctime_s(timebuff, 26, &newtime); if (err) { printf ( "Invalid argument to asctime_s."); exit(1); } ofstream renew ("renewBooks.txt", std::ios::app); renew << "Date: " << "\nTime: " << "\nDue date: "; renew printf( "%.19s %s\n", timebuff, am_pm ); renew << "\nBook barcode: " << toRenewItem; renew.close(); return false; } }; ------ Build started: Project: Lib. Application Framework, Configuration: Debug Win32 ------ Compiling... mainSystemFramework.cpp c:\...\renew.h(24) : error C2065: '_time64_t' : undeclared identifier c:\...\renew.h(24) : error C2146: syntax error : missing ';' before identifier 'long_time' c:\...\renew.h(24) : error C2065: 'long_time' : undeclared identifier c:\...\renew.h(29) : error C2065: 'long_time' : undeclared identifier c:\...\renew.h(31) : error C2065: 'long_time' : undeclared identifier c:\...\renew.h(58) : error C2146: syntax error : missing ';' before identifier 'printf' Lib. Application Framework - 6 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
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...
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...
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
So using _time64_t isn't necessary. Just use standard time_t and your program will be find.
C++ Syntax (Toggle Plain Text)
// // extracted from time.h // #ifndef _TIME_T_DEFINED #ifdef _USE_32BIT_TIME_T typedef __time32_t time_t; /* time value */ #else typedef __time64_t time_t; /* time value */ #endif #define _TIME_T_DEFINED /* avoid multiple def's of time_t */ #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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
On Windows try this (with #include <windows.h>, of course):
Let a user plays with his/her preferred text editor...
c++ Syntax (Toggle Plain Text)
void editFile(const char* filepath) { if (filepath) { HWND wnd = ::GetDesktopWindow(); ::ShellExecute(wnd,"open",filepath,0,0,0); } }
Ot, then, i tried to add days to my date by putting in:
and then, I got a huge runtime error. I took it out, and it was fine. Is there a better way?
C++ Syntax (Toggle Plain Text)
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?
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: POSIX Message Queue
- Next Thread: #include "backward_warning.h"
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






