首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否未在此范围内声明strerror_s、strcpy_s、localtime_s、sprintf_s?

是否未在此范围内声明strerror_s、strcpy_s、localtime_s、sprintf_s?
EN

Stack Overflow用户
提问于 2018-03-30 16:14:17
回答 2查看 4.6K关注 0票数 3

我写了一个跨平台的代码,给出当前日期(mm/dd/yy)和时间(hh/mm/ss)和完整日期(Yyyymmdd),这个代码可以在windows(MSVS2015)下运行,但不能在Linux(GCC 4.8.5)下运行。

我的代码是

代码语言:javascript
运行
复制
#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;
}

//下面是我的main函数

代码语言:javascript
运行
复制
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;
} 

Linux中的错误是

代码语言:javascript
运行
复制
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");

本程序在(windows )visual studio 2015和(Linux) GCC 4.8.5上运行。

我包含了所有必需的头文件,但在Linux中编译时显示错误。

为什么在Linux上会出现上述错误,请告诉我。

EN

回答 2

Stack Overflow用户

发布于 2018-03-30 16:25:59

strcpy_s和friends是微软对c语言的扩展,它们在C11中是标准化的。GCC 4.8.5不支持它们,更新的版本可能也不支持When will the safe string functions of C11 be part of glibc?

票数 4
EN

Stack Overflow用户

发布于 2018-03-30 16:26:09

sprintf_s和其他_s函数不是标准C++的一部分。您的程序将被限制为具有这些功能作为非标准扩展的编译器。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49570839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档