2015-09-04 86 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 
相关问题