By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

Help needed with this code

P: 48
Hi all,

I am presently working with folder and would like to find out the number of files in that particular directory.
But I am getting the following error:

error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

the code is as follows:
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #define _WIN32_WINNT 0x0400
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <string>
  6. using namespace std;
  8. int main(int argc, char *argv[])
  9. {
  10. WIN32_FIND_DATA FindFileData;
  11. HANDLE hFind;
  13. string str1 = "abc.txt";
  15. printf ("Target file is %s.\n", str1.c_str());
  16. hFind = FindFirstFile(str1.c_str(), &FindFileData);
  17. if (hFind == INVALID_HANDLE_VALUE)
  18. {
  19. printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError());
  20. return (0);
  21. }
  22. else
  23. {
  24. printf ("The first file found is %s\n", FindFileData.cFileName);
  25. FindClose(hFind);
  26. return (1);
  27. }
  28. }
any help is appreciated!

Aug 7 '07 #1
Share this Question
Share on Google+
2 Replies

P: 86
It's because you are compiling in Unicode but passing an ANSI string to your function. Use a wstring instead. and wrap your literals in TEXT("your string")

Other suggestions if you really want to make it Unicode neutral is:

1. use TCHAR.H

2. create a neutral string type like this: typedef tstring basic_string<TCHAR>;

3. use int _tmain(INT argc, _TCHAR **argv instead of int main...
Aug 7 '07 #2

Expert Mod 2.5K+
P: 4,677
And please have a look at the Posting Guidelines in the meantime. Thanks!
Aug 7 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.