我这里有个问题。此函数:
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);
}
给我这些关于‘make’的错误:
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*
,因为我得到了很多关于常量的错误。但我真的想摆脱这些错误。
请给我一些建议或更深入的解释为什么会发生这种情况。
发布于 2014-11-03 17:26:37
这些不是您收到警告消息的错误,您的程序将正常工作。
在c++11中,你使用的初始化字符串是不正确的。
查看其他答案,也可以使用std::string str="xxx";
但是你需要包含头文件
发布于 2014-11-03 17:31:37
C++中的字符串文字具有常量字符数组的类型。因此,如果要将字符串文字赋值给字符指针,则必须使用指向常量字符的指针。
因此,更正确的做法是这样写
const char *szExt = "xxx";
或者,如果不想使用限定符const,则可以使用字符数组而不是指针
char szExt[] = "xxx";
考虑到任何修改字符串文字的尝试都会导致未定义的行为。
https://stackoverflow.com/questions/26711145
复制相似问题