在编译以下代码时如何消除此错误和其他错误?
#include "stdafx.h"
#include <string>
#include <vector>
#include <windows.h>
#include <atlstr.h>
#include <tchar.h>
#include <stdio.h>
#define MAX_PATH_LENGTH 256
int main(int argc, char *argv[])
{
int i;
char path[300];
bool FindFilesFromFolder();
getchar();
return 0;
}
bool FindFilesFromFolder()
{
HANDLE hFile;
WIN32_FIND_DATA FindFileData;
std::vector<char> fileList;
char chFolderpath[_MAX_PATH];
CString strExtension = _T("*.B11");
strcpy(chFolderpath, _T("F:\\test\\"));
strcat(chFolderpath, strExtension);
hFile = FindFirstFile(chFolderpath, &FindFileData);
if (hFile == INVALID_HANDLE_VALUE) {
AfxMessageBox(_T("Inavlid file handle."));
return false;
}
CString filepath;
do
{
filepath.Format(_T("%s%s"), _T("F:\\test\\"), FindFileData.cFileName);
fileList.push_back(filepath);
} while(FindNextFile(hFile, &FindFileData));
return true;
}
是的,这是一个了解正确用法的示例findfirstfile()
. I got strcpy
也有错误。
Error:error C2664: 'strcpy' : cannot convert parameter 2 from 'const wchar_t [9]' to 'const char *'. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast.
Error: error C2664: 'strcat' : cannot convert parameter 2 from 'ATL::CString' to 'const char *'. No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.
Error:error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'ATL::CString' to 'const char &' with
它给出了所有转换错误;如何解决这些错误?
你的标题说strcpy
但你的代码和错误有FindFirstFile
?
无论如何,像这样声明你的缓冲区会有所帮助:
TCHAR chFolderpath[_MAX_PATH];
那么你会想要使用_tcscpy
and _tcscat
代替strcpy
and strcat
. 更好的是,#include <strsafe.h>
并使用防止缓冲区溢出的字符串函数 http://msdn.microsoft.com/en-us/library/windows/desktop/ff468908.aspx.
关于AfxMessageBox
,这不是标准的 Windows 函数。它是 MFC 的一部分,我想您是从使用 MFC 的示例中剪切并粘贴的。 Windows 有一个MessageBox http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505.aspx您可以使用该函数,但您需要提供所有参数(父窗口、消息、标题、按钮)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)