简体   繁体   中英

c++ FindFirstFile Cannot convert const char to basic_string

FindFirstFile function somehow doesn't accept my wstring (nor string) to be passed as a parameter.

I get a compiler error

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;
}

I get a compiler error

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

You need a wide char literal to initialize a std::wstring properly:

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

Also you have missed to put another <<

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

Also note that output formatting with std::ostream is different from printf() format string styles. Note I removed the %s from the above sample.

Change

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

hFind = FindFirstFile(path, &dataFile);    // Pass the string directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM