我有C++這是工作的罰款用gcc 4.1.2版20071124但gcc版本給予警告4.4.6 20120305.請建議的一段代碼:警告:從字符串常量棄用轉換爲ACHAR * A
代碼:
WriteLog(sgca_log_file,"Exception occured in load_sgsn_cdr_arch \n",true);
警告:deprecated conversion from string constant to '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");
}
}
是'WriteLog'你的函數:
通過改變你的函數擺脫警告?聽起來像前兩個參數應該比'char *'更多的是'const char *'。 – chris 2013-03-26 06:29:30
請顯示更多的代碼。順便說一下,GCC的最新版本是4.8,比以前的版本更符合標準。 – 2013-03-26 06:46:14
'WriteLog'不應該導致該錯誤。你確定沒有使用另一個'WriteLog'聲明而不是這個聲明嗎?例如,一個頭中沒有'const',而你的定義有'const'? – molbdnilo 2013-03-26 08:36:15