c++中的FindFirstFile()错误C2664

Error C2664 with FindFirstFile() in c++

本文关键字:错误 C2664 中的 FindFirstFile c++      更新时间:2023-10-16

箭头是如果不是字符串s我写什么是存储在字符串s ("C:/Users/John/Desktop/mama/*"),它工作得很好,但我需要这样使用它,因为我将从用户获得路径。我怎么能使它与字符串的工作?错误:错误1错误C2664: 'FindFirstFileA':无法将参数1从'std::string'转换为'LPCSTR'

同样在此错误之前出现了另一个类似的错误,我将字符集从Unicode更改为Not set以使其工作。这个变化会给我的主要项目带来麻烦吗?这是一个测试,当它将是好的,我会把它添加到我的哈希表项目!谢谢你的时间:)

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include <stdio.h>
using namespace std;
struct pathname
{
    string path;
    pathname *next;
};

int main()
{
    pathname *head = new pathname;
    pathname *temp = head;
    WIN32_FIND_DATA fData;
    string s = "C:/Users/John/Desktop/mama/*";
    void * handle = FindFirstFile( s, &fData ); //<~~~~~~
    FindNextFile(handle, &fData);//meta apo auto emfanizei ta onomata
    FindNextFile(handle, &fData);
    temp->next = 0;
    temp->path = fData.cFileName;
    while (FindNextFile(handle, &fData) != 0) //swsto listing
    {   
          temp->next = new pathname;
          temp = temp->next;
          temp->next = 0;
          temp->path = fData.cFileName;
    }
    temp = head;
    cout <<"edw arxizei" <<endl;
    while(temp != 0)
    {
        cout << temp->path << endl;
        temp = temp->next;
    }
    system("pause");
}

std::string没有到const char*的隐式转换。您必须通过调用std::stringc_str()成员函数手动检索指针。

void * handle = FindFirstFile( s.c_str(), &fData );

使用微软的Unicode是没有用的。你必须切换到使用std::wstring,并且仍然必须调用c_str()来检索指向字符串缓冲区的原始指针。

您可以在cppreference.com上找到更多关于各种字符串及其操作的信息