在c ++中使用FindFirstFile()的错误C2664

时间:2013-04-17 02:57:19

标签: c++ file

如果箭头是if而不是字符串si写出存储在字符串s中的内容(“C:/ Users / John / Desktop / mama / *”)它运行良好但我需要像这样使用它因为我会得到来自用户的路径。我怎样才能使用字符串?错误:错误1错误C2664:'FindFirstFileA':无法将参数1从'std :: string'转换为'LPCSTR'

此错误之前还出现了另一个类似的错误,我将字符集从Unicode更改为Not设置以使其工作。这种变化是否会给我的主项目带来麻烦?这是一个测试,当它没问题时我会将它添加到我的哈希表项​​目中! 谢谢您的时间:))

#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");
}

1 个答案:

答案 0 :(得分:4)

std::string没有隐式转换为const char*。您必须通过调用c_str()的{​​{1}}成员函数来手动检索指针。

std::string

改变微软的Unicode思想无济于事。您必须切换到使用void * handle = FindFirstFile( s.c_str(), &fData ); ,仍然必须调用std::wstring来检索指向字符串缓冲区的原始指针。

您可以在cppreference.com

上找到有关各种字符串及其操作的更多信息