错误C2664:'strcpy':不能将参数2从'const wchar_t [9]'转换为'const char *'。如何解决这个错误? [英] Error C2664: 'strcpy' : cannot convert parameter 2 from 'const wchar_t [9]' to 'const char *'.How to solve this error?
问题描述
如何在编译以下代码时摆脱此错误和其他错误?
How can I get rid of this error and other while compiling the below code?
#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;
}
是的,这是一个例子, code> findfirstfile()。我也有 strcpy
错误。
Yes this is an example to know the correct usage of findfirstfile()
. I got strcpy
errors too.
错误:错误C2664:'strcpy':不能将参数2从'const wchar_t [9]'转换为'const char *'。指向的类型是不相关的;转换需要reinterpret_cast,C风格的转换或函数式转换。
错误:error C2664:'strcat' :不能将参数2从'ATL :: CString'转换为'const char *'。没有可以执行此转换的用户定义转换运算符,或者无法调用运算符。
错误:error C2664:'std :: vector< _Ty> :: push_back':无法将参数1从'ATL :: CString'转换为'const char&'with
它提供所有转换错误;如何解决这些错误?
It gives all conversion errors; how to solve these errors?
推荐答案
您的标题为 strcpy
代码和错误有 FindFirstFile
?
Your title says strcpy
but your code and errors have FindFirstFile
?
无论如何,声明这样的缓冲区将有助于:
Anyway, declaring your buffer like this will help:
TCHAR chFolderpath[_MAX_PATH];
然后您将要使用 _tcscpy
和 _tcscat
而不是 strcpy
和 strcat
/ strike>更好, #include< strsafe.h>
并使用
Then you will want to use Even better, _tcscpy
and _tcscat
instead of strcpy
and strcat
.#include <strsafe.h>
and use the string functions that protect against buffer overrun.
关于 AfxMessageBox
,这不是一个标准的Windows功能。它是MFC的一部分,我想你从一个使用MFC的示例剪切和粘贴。 Windows具有 MessageBox
函数,你可以使用,但你需要提供所有的参数(父窗口,消息,标题,按钮)。
Regarding AfxMessageBox
, this is not a standard Windows function. It's part of MFC, I suppose you cut and pasted from an example that used MFC. Windows has a MessageBox
function which you can use, but you'll need to supply all the parameters (parent window, message, title, buttons).
这篇关于错误C2664:'strcpy':不能将参数2从'const wchar_t [9]'转换为'const char *'。如何解决这个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!