-1

Where the arrow is if instead of string s i write what is stored in string s ( "C:/Users/John/Desktop/mama/*" ) it works well but i need to use it like this because i will get the path from the user. How can i make it work with a string? Error : Error 1 error C2664: 'FindFirstFileA' : cannot convert parameter 1 from 'std::string' to 'LPCSTR'

Also before this error oneother similar error appeared and i changed character set from Unicode to Not set to make it work. Is this change going to create trouble to my main project? This one is a test and when it will be ok i will add it to my hashtable project! Thank you for your time :)

#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");
}
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
captain monk
  • 719
  • 4
  • 11
  • 34
  • 1
    I should really keep a bookmark of a `std::string` to `LPCSTR` question. There are so many. – chris Apr 17 '13 at 02:58
  • For what it's worth, when I searched SO for "[c++] std::string to c-string", the first equivalent one to come up was one I answered :p (http://stackoverflow.com/questions/10865957/c-printf-with-stdstring) – chris Apr 17 '13 at 03:04

1 Answers1

4

std::string does not have an implicit conversion to const char*. You have to manually retrieve the pointer by calling the c_str() member function of std::string.

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

Changing to Microsoft's idea of Unicode won't help. You'll have to switch to using std::wstring and will still have to call c_str() to retrieve a raw pointer to the string buffer.

You can find out more about the various strings and their operations on cppreference.com

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74