4

I have written a cross-platform code gives a current date(mm/dd/yy) and time(hh/mm/ss) and complete date(yyyymmdd), This code works in windows(MSVS2015) but not working in Linux(GCC 4.8.5).

My code is

#include <iostream> 
#ifdef WIN32
#include <windows.h>
#else
#include <cerrno>
#endif

#ifdef WIN32
#include <direct.h>
#include <io.h>
#else
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#endif
#include <ctime>
#include <bitset>
#include <cstdlib>  /*atol*/
#include <iomanip>
#include <stdexcept>
#include <cstring>
#include <cstdio>
#include <fstream>
using namespace std;

template <size_t size>
void GetNowDateTime(char(&c_date)[size], char(&c_time)[size])
{
   time_t t;
   struct tm now;
   strcpy_s(c_date, "00/00/00");
   strcpy_s(c_time, "00:00:00");
   time(&t);
  if (localtime_s(&now, &t) != 0) return;
 char temp[3];
 sprintf_s(temp, "%.2d", now.tm_mon + 1);
 memcpy(c_date, temp, 2);
 sprintf_s(temp, "%.2d", now.tm_mday);
 memcpy(c_date + 3, temp, 2);
 sprintf_s(temp, "%.2d", now.tm_year - 100);
 memcpy(c_date + 6, temp, 2);
 sprintf_s(temp, "%.2d", now.tm_hour);
 memcpy(c_time, temp, 2);
 sprintf_s(temp, "%.2d", now.tm_min);
 memcpy(c_time + 3, temp, 2);
 sprintf_s(temp, "%.2d", now.tm_sec);
 memcpy(c_time + 6, temp, 2);
 }

  int GetToday(void)
  {
    time_t t;
    struct tm now;
    time(&t);
    if (localtime_s(&now, &t) != 0) return 0;
    return (now.tm_year + 1900) * 10000 + (now.tm_mon + 1) * 100 + now.tm_mday;
  }

 bool OpenOuputFile(ofstream& outputFile)
 {

  char buf[1024];

 #ifdef WIN32
   strcpy_s(buf, "C:\\Myfolder\\output.txt");
  #else
    strcpy_s(buf, "/home/myfolder/output.txt");
    #endif
    outputFile.open(buf, ios::out);

    if (!outputFile)
    {
    char szErrorMsg[1024];
    strerror_s(szErrorMsg, errno);
    cout << "Unable to open input file. " << buf << " Error:" << szErrorMsg << endl;
    return 0;
}
return 1;
}

//Here is my main function

int main()
{
char today[9];
char time[9];
ofstream outputFile;
GetNowDateTime(today, time);
int yyyymmdd = GetToday();
if (OpenOuputFile(outputFile))
{
    outputFile << "GetNowDateTime functions given is:-\t" << today << "\t" << time << endl;
    outputFile << "GetToday Function given is:-\t" << yyyymmdd << endl;
   }
   else
    cout << "No output file written" << endl;
return 0;
} 

Errors in Linux is

TimeDate.cpp: In function ‘void GetNowDateTime(char (&)[size], char (&)[size])’:

TimeDate.cpp:34:26: error: there are no arguments to ‘localtime_s’ that depend on a template parameter, so a declaration of ‘localtime_s’ must be available [-fpermissive]
  if (localtime_s(&now, &t) != 0) return;
TimeDate.cpp:36:40: error: there are no arguments to ‘sprintf_s’ that depend on a template parameter, so a declaration of ‘sprintf_s’ must be available [-fpermissive]
imeDate.cpp: In function ‘int GetToday()’:
TimeDate.cpp:55:26: error: ‘localtime_s’ was not declared in this scope
if (localtime_s(&now, &t) != 0) return 0;
TimeDate.cpp:74:31: error: ‘strerror_s’ was not declared in this scope
strerror_s(szErrorMsg, errno);
TimeDate.cpp:31:29: error: ‘strcpy_s’ was not declared in this scope
strcpy_s(c_date, "00/00/00");

This program runs on (windows )visual studio 2015 and(Linux) GCC 4.8.5.

I included all required headers but It showing errors when compiling in Linux.

why above errors showing in Linux, Please tell me.

jww
  • 97,681
  • 90
  • 411
  • 885
  • Why are you using C string functions in a C++ program? Use `std::string`. – Barmar Mar 30 '18 at 08:18
  • I was compilng other people's C++ code that used a lots of strcpy_s, strcat_s, ... Tried a few trick with gnu compiler could not get rid of the error message. Anyone has a way of making this function legal? Other choice is to convert old C (disguised as C++) into real C++ string operations. – Kemin Zhou Jul 12 '20 at 20:12

2 Answers2

5

strcpy_s and friends were Microsoft extensions to c, they were standardised in C11. GCC 4.8.5 doesn't support them and newer versions probably don't either When will the safe string functions of C11 be part of glibc?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
2

sprintf_s and the other _s functions are not part of Standard C++. Your program will be restricted to compilers which have those functions as a non-standard extension.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • ,I removed _s most of the errors rid but some errors are still remains They are:- **TimeDate.cpp: In function ‘void TimeDate.cpp:42:26: error: cannot convert ‘tm*’ to ‘const time_t* {aka const long int*}’ for argument ‘1’ to ‘tm* localtime(const time_t*)’ if (localtime_s(&now, &t) != 0) return; ^ TimeDate.cpp: In function ‘int GetToday()’: TimeDate.cpp:63:26: error: cannot convert ‘tm*’ to ‘const time_t* {aka const long int*}’ for argument ‘1’ to ‘tm* localtime(const time_t*)’ if (localtime_s(&now, &t) != 0) return 0;** – kusalava binni Mar 30 '18 at 09:16
  • 1
    @kusalava - If you have a new question, please ask a new question. – Bo Persson Mar 30 '18 at 10:12