带有TCHAR和字符串连接的C ++消息框

时间:2013-03-29 19:17:49

标签: c++ messagebox

有人可以告诉我如何在消息框中输出szFileName吗?

我的下方尝试不起作用

//Retrieve the path to the data.dat in the same dir as our app.dll is located

TCHAR szFileName[MAX_PATH+1];
GetModuleFileName(_Module.m_hInst, szFileName, MAX_PATH+1);
StrCpy(PathFindFileName(szFileName), _T("data.dat"));

FILE *file =fopen(szFileName,"rb");
if (file)
{
    fseek( file,iFirstByteToReadPos, SEEK_SET);
    fread(bytes,sizeof(unsigned char), iLenCompressedBytes, file);
    fclose(file);
}
else
{
    MessageBox(NULL, szFileName + " not found", NULL, MB_OK);
    DebugBreak();
}

2 个答案:

答案 0 :(得分:1)

您无法添加:

szFileName + " not found", 

简单修复:

MessageBox(NULL, szFileName, L"File not found", MB_OK);

答案 1 :(得分:1)

C ++不支持'+'来连接char或TCHAR数组。您需要为此使用字符串类,或者使用strcat和堆栈上的缓冲区以C风格方式执行此操作。

由于您使用的是C ++,如果您使用的是ATL / mfc,则可以使用CString,或者您可以使用以下内容:

typedef std::basic_string<TCHAR> tstring;

...
MessageBox(NULL, tstring(szFileName) + " not found", NULL, MB_OK);

通常的C ++管道作为练习留给了读者。

相关问题