Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. FindFirstFile error in compilation [modified]

FindFirstFile error in compilation [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    darkcloud 42o
    wrote on last edited by
    #1

    Hi, Having a little trouble with FindFirst File function.... heres what ive got... include "stdafx.h" #include #include //#include "dirent.h" #include #include #include #include #define WIN32_LEAN_AND_MEAN #include #include #include #pragma comment(lib,"ws2_32") using namespace std; // Stuff....... char fileFound[256]; strcpy(fileFound, path1); WIN32_FIND_DATA FindFileData; HANDLE hp; hp = FindFirstFile(fileFound, &FindFileData); printf ("The first file found is %s\n", FindFileData.cFileName); FindClose(hp); The error i recieve is... error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'char [256]' to 'LPCWSTR' I know sometimes when compiling the error is not always what it seeems.. but have no idea.. Please help. TIA -- modified at 7:45 Saturday 3rd June, 2006

    _ H 2 Replies Last reply
    0
    • D darkcloud 42o

      Hi, Having a little trouble with FindFirst File function.... heres what ive got... include "stdafx.h" #include #include //#include "dirent.h" #include #include #include #include #define WIN32_LEAN_AND_MEAN #include #include #include #pragma comment(lib,"ws2_32") using namespace std; // Stuff....... char fileFound[256]; strcpy(fileFound, path1); WIN32_FIND_DATA FindFileData; HANDLE hp; hp = FindFirstFile(fileFound, &FindFileData); printf ("The first file found is %s\n", FindFileData.cFileName); FindClose(hp); The error i recieve is... error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'char [256]' to 'LPCWSTR' I know sometimes when compiling the error is not always what it seeems.. but have no idea.. Please help. TIA -- modified at 7:45 Saturday 3rd June, 2006

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      can you tell me what type of application are you working on? MFC , Console or On something else because I tried executing your code in MFC application and apart from a very minor change I was able to get the result. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

      D 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        can you tell me what type of application are you working on? MFC , Console or On something else because I tried executing your code in MFC application and apart from a very minor change I was able to get the result. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

        D Offline
        D Offline
        darkcloud 42o
        wrote on last edited by
        #3

        Hi, Im using Visual Studio 2005 but ive figured that part out via searching long and hard on the web... however im stuck with another problem just the same.... heres what i got for that part.... std::wstring s2ws(const std::string& s){ int len; int slength = (int)s.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); std::wstring r(buf); delete[] buf; return r; } #ifdef UNICODE std::wstring stemp = s2ws(ipp); // Temporary buffer is required LPCWSTR result = stemp.c_str(); #endif WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH + 1]; // directory specification DWORD dwError; hFind = FindFirstFile(result, &FindFileData); printf ("First file name is %s\n", FindFileData.cFileName); My problem is i cant seem to get the FindFileData function to return a readable string... but i need FindFileData to be string s; for example...

        _ 1 Reply Last reply
        0
        • D darkcloud 42o

          Hi, Im using Visual Studio 2005 but ive figured that part out via searching long and hard on the web... however im stuck with another problem just the same.... heres what i got for that part.... std::wstring s2ws(const std::string& s){ int len; int slength = (int)s.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); std::wstring r(buf); delete[] buf; return r; } #ifdef UNICODE std::wstring stemp = s2ws(ipp); // Temporary buffer is required LPCWSTR result = stemp.c_str(); #endif WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH + 1]; // directory specification DWORD dwError; hFind = FindFirstFile(result, &FindFileData); printf ("First file name is %s\n", FindFileData.cFileName); My problem is i cant seem to get the FindFileData function to return a readable string... but i need FindFileData to be string s; for example...

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          Should get it now. strcat(result,"\0"); hFind=FindFirstFile(result,&FindFileData); Do you get the name of the file or not? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

          D 1 Reply Last reply
          0
          • _ _AnsHUMAN_

            Should get it now. strcat(result,"\0"); hFind=FindFirstFile(result,&FindFileData); Do you get the name of the file or not? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

            D Offline
            D Offline
            darkcloud 42o
            wrote on last edited by
            #5

            //ok i fixed part of it but still need to get std::string out of it... //here is what should be a compilable version of the code fragment... #include #include #include std::wstring s2ws(const std::string& s){ int len; int slength = (int)s.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); std::wstring r(buf); delete[] buf; return r; } int main(){ string ipp="c:\\"; #ifdef UNICODE std::wstring stemp = s2ws(ipp); // Temporary buffer is required LPCWSTR result = stemp.c_str(); #endif WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH + 1]; // directory specification DWORD dwError; hFind = FindFirstFile(result, &FindFileData); printf ("First file name is %s\n", FindFileData.cFileName); } //The Return is fine i get First file name is . //which is what i think im looking for... however //I still to need to get the string instead of the printed .

            _ 1 Reply Last reply
            0
            • D darkcloud 42o

              //ok i fixed part of it but still need to get std::string out of it... //here is what should be a compilable version of the code fragment... #include #include #include std::wstring s2ws(const std::string& s){ int len; int slength = (int)s.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); std::wstring r(buf); delete[] buf; return r; } int main(){ string ipp="c:\\"; #ifdef UNICODE std::wstring stemp = s2ws(ipp); // Temporary buffer is required LPCWSTR result = stemp.c_str(); #endif WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH + 1]; // directory specification DWORD dwError; hFind = FindFirstFile(result, &FindFileData); printf ("First file name is %s\n", FindFileData.cFileName); } //The Return is fine i get First file name is . //which is what i think im looking for... however //I still to need to get the string instead of the printed .

              _ Offline
              _ Offline
              _AnsHUMAN_
              wrote on last edited by
              #6

              what string do you need . The filename is in FindFileData.cFileName Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

              D 1 Reply Last reply
              0
              • _ _AnsHUMAN_

                what string do you need . The filename is in FindFileData.cFileName Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

                D Offline
                D Offline
                darkcloud 42o
                wrote on last edited by
                #7

                heres all i could come up with with the find filedata... string s; s=(FindFileData.cFileName); cout << s; no luck.. error C2679: binary '=' : no operator found which takes a right-hand operand of type 'WCHAR [260]' (or there is no acceptable conversion)

                _ J 2 Replies Last reply
                0
                • D darkcloud 42o

                  heres all i could come up with with the find filedata... string s; s=(FindFileData.cFileName); cout << s; no luck.. error C2679: binary '=' : no operator found which takes a right-hand operand of type 'WCHAR [260]' (or there is no acceptable conversion)

                  _ Offline
                  _ Offline
                  _AnsHUMAN_
                  wrote on last edited by
                  #8

                  char *s=new char[255]; strcpy(s,FindFileData.cFileName); cout<_AnShUmAn_

                  1 Reply Last reply
                  0
                  • D darkcloud 42o

                    heres all i could come up with with the find filedata... string s; s=(FindFileData.cFileName); cout << s; no luck.. error C2679: binary '=' : no operator found which takes a right-hand operand of type 'WCHAR [260]' (or there is no acceptable conversion)

                    J Offline
                    J Offline
                    Justin Tay
                    wrote on last edited by
                    #9

                    std::string stores char. You need to use std::wstring to store wchar_t. You probably need to read up on unicode. If it's not your intention to use unicode set your project to use multibyte character strings instead.

                    D 1 Reply Last reply
                    0
                    • J Justin Tay

                      std::string stores char. You need to use std::wstring to store wchar_t. You probably need to read up on unicode. If it's not your intention to use unicode set your project to use multibyte character strings instead.

                      D Offline
                      D Offline
                      darkcloud 42o
                      wrote on last edited by
                      #10

                      strcpy not working... researching info for converting wstring to string.... no luck yet...

                      D 1 Reply Last reply
                      0
                      • D darkcloud 42o

                        strcpy not working... researching info for converting wstring to string.... no luck yet...

                        D Offline
                        D Offline
                        darkcloud 42o
                        wrote on last edited by
                        #11

                        found it... wstring s= s; string ssss; s=FindFileData.cFileName; ssss.assign(s.begin(), s.end()); cout << ssss; Thnx for your help

                        J 1 Reply Last reply
                        0
                        • D darkcloud 42o

                          found it... wstring s= s; string ssss; s=FindFileData.cFileName; ssss.assign(s.begin(), s.end()); cout << ssss; Thnx for your help

                          J Offline
                          J Offline
                          Justin Tay
                          wrote on last edited by
                          #12

                          But then the question you should ask yourself is this. Why are you building a unicode application in the first place?

                          1 Reply Last reply
                          0
                          • D darkcloud 42o

                            Hi, Having a little trouble with FindFirst File function.... heres what ive got... include "stdafx.h" #include #include //#include "dirent.h" #include #include #include #include #define WIN32_LEAN_AND_MEAN #include #include #include #pragma comment(lib,"ws2_32") using namespace std; // Stuff....... char fileFound[256]; strcpy(fileFound, path1); WIN32_FIND_DATA FindFileData; HANDLE hp; hp = FindFirstFile(fileFound, &FindFileData); printf ("The first file found is %s\n", FindFileData.cFileName); FindClose(hp); The error i recieve is... error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'char [256]' to 'LPCWSTR' I know sometimes when compiling the error is not always what it seeems.. but have no idea.. Please help. TIA -- modified at 7:45 Saturday 3rd June, 2006

                            H Offline
                            H Offline
                            Hamid Taebi
                            wrote on last edited by
                            #13

                            your Character set is Unicode Character Set LPTSTR lpc; lpc=(LPTSTR)LocalAlloc(LPTR,256); wsprintf(lpc,_T("%s"), _T("c:\\code.txt")); WIN32_FIND_DATA FindFileData; HANDLE hp; hp = FindFirstFile(lpc, &FindFileData); LocalFree(lpc);_**


                            **_

                            whitesky


                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups