2013-04-17 44 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