Error C2664: 'strcpy': no ​​se puede convertir el parámetro 2 de 'const wchar_t [9]' a 'const char *'. ¿Cómo solucionar este 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;

  }

Yes this is an example to know the correct usage of findfirstfile(). tengo strcpy errors too.

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

It gives all conversion errors; how to solve these errors?

preguntado el 01 de febrero de 12 a las 03:02

You can't use declare char arrays when working with Unicode strings. Use either the TCHAR macro to handle it automatically, or declare it explicitly as a wide string with wchar_t. -

Please look at the preview pane and fix your formatting before submission. It only takes a moment of your time, and saves a few of ours. -

Además, esto no tiene nada que ver con c. -

1 Respuestas

Your title says strcpy but your code and errors have FindFirstFile?

Anyway, declaring your buffer like this will help:

TCHAR chFolderpath[_MAX_PATH];

Then you will want to use _tcscpy y _tcscat en lugar de strcpy y strcat. Aun mejor, #include <strsafe.h> y use the string functions that protect against buffer overrun.

Con respecto a 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).

Respondido 01 Feb 12, 07:02

Yes this is an example to know the correct usuageof findfirstfile(). - día

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.