sajal
sajal

Reputation: 69

warning: deprecated conversion from string constant to âchar*â

I have a piece of code in c++ which is working fine with gcc version 4.1.2 20071124 but giving warning with gcc version 4.4.6 20120305. Please suggest:

Code:

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

Warning: deprecated conversion from string constant to 'char*'

where Writelog is a function to print logs in specified log file:

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");
    }
}

Upvotes: 2

Views: 2277

Answers (2)

Kyurem
Kyurem

Reputation: 1869

This is because string literals are non-modifiable. But for backwards compatibility they are allowed to be treated as char* instead of const char*.

Get rid of the warning by changing your function to:

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

Upvotes: 1

I would declare

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

because very probably you don't change the fileName or the pLogMsg

Upvotes: 3

Related Questions