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:\...\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)

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.

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.

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).

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

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...

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

//
// 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.

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!

On Windows try this (with #include <windows.h>, of course):

void editFile(const char* filepath)
{
    if (filepath) {
        HWND wnd = ::GetDesktopWindow();    
        ::ShellExecute(wnd,"open",filepath,0,0,0);
    }
}

Let a user plays with his/her preferred text editor...

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

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?

After making that adjustment you have to call mktime() to normalize the structure. Otherwise, I don't know what you mean, so post latest code.

Ok then... Here is the latest code (with the thing that causes the runtime error):

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";
		time_t long_time;
		char timebuff [26];
		errno_t err;

		//Get time as a 64 bit integer
		time( &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;

		// This is where the error is:[
		newtime.tm_mday += 14;
		// ]This is where the error ends
		
		// 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;
	}
};

It's not newtime.tm_mday += 14; statement failed: the next one. The asctime_s assertion failed when the function processes tm_mday == 40. Obviously, this future day of month (26+14) is invalid.

To normalize "moved forward" newtime structure add some conversions:

newtime.tm_mday += 14;
    // mktime can accept "bad" day of month
    time_t xtime = mktime(&newtime);
    // refresh newtime structure
    _localtime64_s(&newtime,&xtime);

Of course, you may rewrite this snippet with those funny *_s "safety" functions and long time types...

Thank you soooooooo much guys to take the time out of your day to help me with my silly questions. +1 reputation :)

This article has been dead for over six months. Start a new discussion instead.