2012-02-01 6 views
-1

以下のコードをコンパイルする際に、このエラーを取り除くにはどうすればよいですか?エラーC2664: 'strcpy': 'const wchar_t [9]'から 'const char *'にパラメータ2を変換できません。

#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()です。私も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

これは、すべての変換エラーが発生します。どのようにこれらのエラーを解決するには?

+2

Unicode文字列を扱う際に 'char'配列を宣言することはできません。自動的に処理するには 'TCHAR'マクロを使用するか、' wchar_t'でワイド文字列として明示的に宣言してください。 –

+0

提出前にプレビューペインを見て、書式を修正してください。それはあなたの時間のほんの一瞬しかかかりません、私たちのいくつかを保存します。 –

+0

また、これは 'c'とは関係ありません。 –

答えて

3

あなたのタイトルはstrcpyですが、あなたのコードとエラーはFindFirstFileですか?

とにかく、このようなあなたのバッファを宣言するのに役立ちます:

TCHAR chFolderpath[_MAX_PATH]; 

その後、あなたの代わりに strcpystrcat_tcscpy_tcscatを使用したいと思うでしょう。 さらに、#include <strsafe.h>the string functions that protect against buffer overrunを使用してください。

AfxMessageBoxについては、これは標準のWindows機能ではありません。これはMFCの一部です。MFCを使用した例を切り取って貼り付けたとします。 WindowsにはMessageBoxという機能がありますが、すべてのパラメータ(親ウィンドウ、メッセージ、タイトル、ボタン)を指定する必要があります。

+0

はいこれは、findfirstfile()の正しい使用法を知るための例です。 – hari

関連する問題