gpt4 book ai didi

c++ - 警告 : deprecated conversion from string constant to âchar*â

转载 作者:太空狗 更新时间:2023-10-29 11:45:57 25 4
gpt4 key购买 nike

我有一段 c++ 代码,它在 gcc 版本 4.1.2 20071124 中工作正常,但在 gcc 版本 4.4.6 20120305 中给出警告。请建议:

代码:

WriteLog(sgca_log_file,"Exception occured in load_sgsn_cdr_arch \n",true);

警告:弃用了从字符串常量到“char*”的转换

其中 Writelog 是在指定日志文件中打印日志的函数:

void WriteLog(const char* fileName, const char* pLogMsg, bool pTimeRequired)
{
FILE *lFileDesc = NULL;
char lMessage[1000];
char lBuffer[1000];
char lDate[1000];
time_t lRawtime;
struct tm * lTimeinfo;
char LoaderLogFile[80];
char loaderHome[30];
char * pch;
char szdir[30];

try
{
memset(LoaderLogFile, '\0',80);
memset(lMessage, '\0', 1000);
memset(lBuffer, '\0', 1000);
memset(lDate, '\0', 1000);

time(&lRawtime);
lTimeinfo = localtime(&lRawtime);
strftime(lBuffer, 1000, "| %x - %X | ", lTimeinfo);
strftime(lDate, 1000, "%Y_%m_%d", lTimeinfo);

if (!strcmp(fileName,"default"))
{
strcpy(loaderHome, getenv("LOADER_HOME"));
sprintf(LoaderLogFile,"%s/log/Loader_%s.log",loaderHome,lDate);
}

else
sprintf(LoaderLogFile,"%s_%s.log",fileName,lDate);

if (!file_exists(LoaderLogFile))
{
if((pch=strrchr(LoaderLogFile,'/')) != '\0')
{
strncpy(szdir,LoaderLogFile,pch-LoaderLogFile+1);
if(file_exists(szdir))
{
lFileDesc = fopen(LoaderLogFile, "a");
fflush(lFileDesc);
fclose(lFileDesc);
}
else
{
sprintf (lBuffer,"Directory %s doesnot exist. Please check the configurations. Stopping the System. \n",szdir);
WriteLog("default",lBuffer,true);
StopSystem("default",lBuffer);
}
}
else
{
sprintf (lBuffer,"Invalid log file name %s \n",LoaderLogFile);
WriteLog("default",lBuffer,true);
StopSystem("default",lBuffer);
}

}

//! Check whether the timestamp also has to be written in the log file
if(pTimeRequired)
{
sprintf(lMessage, "%s%s\n", lBuffer, pLogMsg);
}
else
{
sprintf(lMessage, "%s\n", pLogMsg);
}

//! Open the log file in append mode
lFileDesc = fopen(LoaderLogFile, "a");
if(lFileDesc != NULL)
{
fprintf(lFileDesc, lMessage);
fflush(lFileDesc);
fclose(lFileDesc);
}
else
{
printf("Unable to open the file \n");
}



}
catch(...)
{
printf("Exception occured in WriteLog \n");
}
}

最佳答案

我要声明

   void WriteLog(const char* fileName, const char* pLogMsg, 
bool pTimeRequired);

因为您很可能不会更改 fileNamepLogMsg

关于c++ - 警告 : deprecated conversion from string constant to âchar*â,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15630712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com