我在这里有一个问题。 此功能:函数与C++ 11规范不兼容
BOOL WINAPI SFileGetFileName(HANDLE hFile, char * szFileName) {
TMPQFile * hf = (TMPQFile *)hFile; // MPQ File handle
char *szExt = "xxx"; // Default extension
DWORD dwFirstBytes[2]; // The first 4 bytes of the file
DWORD dwFilePos; // Saved file position
int nError = ERROR_SUCCESS;
int i;
// Pre-zero the output buffer
if(szFileName != NULL)
*szFileName = 0;
// Check valid parameters
if(nError == ERROR_SUCCESS)
{
if(hf == NULL || szFileName == NULL)
nError = ERROR_INVALID_PARAMETER;
}
// If the file name is already filled, return it.
if(nError == ERROR_SUCCESS && *hf->szFileName != 0)
{
if(szFileName != hf->szFileName)
strcpy(szFileName, hf->szFileName);
return TRUE;
}
if(nError == ERROR_SUCCESS)
{
if(hf->dwBlockIndex == (DWORD)-1)
nError = ERROR_CAN_NOT_COMPLETE;
}
// Read the first 8 bytes from the file
if(nError == ERROR_SUCCESS)
{
dwFirstBytes[0] = dwFirstBytes[1] = 0;
dwFilePos = SFileSetFilePointer(hf, 0, NULL, FILE_CURRENT);
SFileReadFile(hFile, &dwFirstBytes, sizeof(dwFirstBytes), NULL);
BSWAP_ARRAY32_UNSIGNED(dwFirstBytes, sizeof(dwFirstBytes)/sizeof(DWORD));
SFileSetFilePointer(hf, dwFilePos, NULL, FILE_BEGIN);
}
if(nError == ERROR_SUCCESS)
{
if((dwFirstBytes[0] & 0x0000FFFF) == ID_EXE)
szExt = "exe";
else if(dwFirstBytes[0] == 0x00000006 && dwFirstBytes[1] == 0x00000001)
szExt = "dc6";
else
{
for(i = 0; id2ext[i].szExt != NULL; i++)
{
if(id2ext[i].dwID == dwFirstBytes[0])
{
szExt = id2ext[i].szExt;
break;
}
}
}
// Create the file name
sprintf(hf->szFileName, "File%08lu.%s", hf->dwBlockIndex, szExt);
if(szFileName != hf->szFileName)
strcpy(szFileName, hf->szFileName);
}
return (nError == ERROR_SUCCESS);
}
给我上 '制作' 这些错误:
SFileReadFile.cpp: In function ‘bool SFileGetFileName(HANDLE, char*)’:
SFileReadFile.cpp:655:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *szExt = "xxx"; // Default extension
^
SFileReadFile.cpp:700:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
szExt = "exe";
^
SFileReadFile.cpp:702:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
szExt = "dc6";
^
SFileReadFile.cpp:716:72: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘DWORD {aka unsigned int}’ [-Wformat=]
sprintf(hf->szFileName, "File%08lu.%s", hf->dwBlockIndex, szExt);
请给我提示,如何解决这些?
我已经在C++文档中尝试了很多,但我没有太多的运气来找到需要的东西。看,我不能在szext
的声明中做const char const*
,因为我得到很多关于常量的错误。但我真的想摆脱这些错误。
请给我一些建议或为什么发生更深的解释。
'char const * szExt =“xxx”;'工作吗? (注意额外的'const')。一个字符串数组“'xx”'是一个'char const []',并且转换到'char *'的过程大概是C++ 14。使用'char const *'应该是首选。 – Niall 2014-11-03 09:24:38
你说得对,'szExt'不应该被定义为'const char * const szExt',但是你有什么建议让你觉得这样做?了解你的理解将允许更有用的答案。 – hvd 2014-11-03 09:24:48
@Niall是的,没错,但不幸的是,像这样的大多数问题,只是给出正确的答案并没有帮助任何人真正理解正在发生的事情。 (也许这里有所不同。) – hvd 2014-11-03 09:26:28