2013-04-17 41 views
-1

如果箭头是如果而不是字符串si写什么存储在字符串s(“C:/用户/约翰/桌面/妈妈/ *”)它运作良好,但我需要像这样使用它,因为我将得到用户的路径。我怎样才能使它与字符串一起工作?错误:错误1错误C2664:'FindFirstFileA':无法将参数1从'std :: string'转换为'LPCSTR'错误C2664与FindFirstFile()在C++

也出现此错误之后出现了另一个类似的错误,我将Unicode字符集更改为未设置为这行得通。这种改变是否会给我的主要项目带来麻烦?这是一个测试,什么时候可以,我会将它添加到我的哈希表项​​目中! 感谢您的时间:)

#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

我要一个'的std :: string'的书签真守为'LPCSTR'问题。有这么多。 – chris

+0

为什么它的价值,当我搜索“[C++] std :: string到c-string”时,第一个等价的是我回答:p(http://stackoverflow.com/questions/10865957/C-printf的与 - stdstring) – chris

回答

4

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

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

更改为微软的Unicode的想法将无济于事。您必须切换到使用std::wstring,并且仍然必须调用c_str()来检索指向字符串缓冲区的原始指针。

您可以了解更多有关的各种字符串,并将其操作上cppreference.com