Help needed with this code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flavourofbru
    New Member
    • May 2007
    • 48

    Help needed with this code

    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:
    [code=c]
    #include "stdafx.h"
    #define _WIN32_WINNT 0x0400
    #include <windows.h>
    #include <stdio.h>
    #include <string>
    using namespace std;

    int main(int argc, char *argv[])
    {
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    string str1 = "abc.txt";

    printf ("Target file is %s.\n", str1.c_str());
    hFind = FindFirstFile(s tr1.c_str(), &FindFileDat a);
    if (hFind == INVALID_HANDLE_ VALUE)
    {
    printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError()) ;
    return (0);
    }
    else
    {
    printf ("The first file found is %s\n", FindFileData.cF ileName);
    FindClose(hFind );
    return (1);
    }
    }[/code]

    any help is appreciated!

    Thanks!!
    Last edited by sicarie; Aug 7 '07, 01:52 PM. Reason: Code tags are on the right when creating/replying to a thread. Please use them.
  • Darryl
    New Member
    • May 2007
    • 86

    #2
    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<TC HAR>;

    3. use int _tmain(INT argc, _TCHAR **argv instead of int main...

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      And please have a look at the Posting Guidelines in the meantime. Thanks!

      Comment

      Working...