2015-09-04 87 views
0

FindFirstFile函數以某種方式不接受我的wstring(也不是字符串)作爲參數傳遞。C++ FindFirstFile無法將常量字符轉換爲basic_string

我得到一個編譯錯誤

Cannot convert const char[9] to std::basic_string 

#include "stdafx.h" 
#include <string> 
#include <iostream> 
#include <stdio.h> 
#include <Windows.h> 


using namespace std; 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    wstring path = "C:\\*.dmp"; 
    WIN32_FIND_DATA dataFile; 
    HANDLE hFind; 

    hFind = FindFirstFile (path.c_str(), &dataFile); 


    cout << "The name of the first found file is %s \n" dataFile.cFileName << endl; 
    FindClose hFind; 
    getchar(); 
    return 0; 
} 

回答

1

我得到一個編譯錯誤

Cannot convert const char[9] to std::basic_string 

你需要一個wide char literal正確初始化一個std::wstring

wstring path = L"C:\\*.dmp"; 
      //^

而且你已經錯過了把另一個<<

cout << "The name of the first found file is " << dataFile.cFileName << endl;` 
              // ^^ 

還要注意的是output formatting with std::ostreamprintf()格式字符串風格不同。注意我從上面的示例中刪除了%s

+0

@Muteking這是另外一個我先回答,再看一看。 –

+0

幾乎完美的潘塔雷,但現在這個錯誤蔓延:語法錯誤';'在hFind標識符之前缺失。(在endl後) – Muteking

+0

@Muteking您可能錯過了包含其他內容,FindClose來自哪裏?無論如何,這與你原來的問題無關。另一個錯誤,請另一個問題。我不是來幫助吸血鬼的。 –

0

變化

const char path[] = "C:\\*.dmp";   // C-style string 

hFind = FindFirstFile(path, &dataFile); // Pass the string directly 
相關問題