sscanf_s cannot convert ...from 'WCHAR[260] to 'const char *' error.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akino877
    New Member
    • Nov 2007
    • 37

    sscanf_s cannot convert ...from 'WCHAR[260] to 'const char *' error.

    Hello,

    I have a small program which uses FindFirstFile() to get a file name. Once I get the file name, I would like to use sscanf to extract certain part of the file name.

    My program is as follows :

    ......
    int _tmain(int argc, _TCHAR* argv[])
    {
    WIN32_FIND_DATA ffd;
    TCHAR szDir[MAX_PATH];
    HANDLE hFind = INVALID_HANDLE_ VALUE;
    DWORD dwError = 0;
    char filename[128];
    char ext[128];

    StringCchCopy (szDir, MAX_PATH, TEXT(".\\*.dat" ));
    hFind = FindFirstFile (szDir, &ffd);
    if (hFind == INVALID_HANDLE_ VALUE)
    {
    dwError = GetLastError();
    if (dwError == ERROR_PATH_NOT_ FOUND)
    _tprintf (TEXT("[Error] Directory not found\n");
    else
    _tprintf (TEXT("FindFirs tFile failed (%u)\n", dwError);
    return dwError;
    }
    else
    {
    _tprintf(TEXT ("File name : %s\n"), ffd.cFileName);
    if (sscanf_s (ffd.cFileName, "%16s.%3s", filename, ext) == 2)
    {
    printf("File Name : %s\n", filename);
    printf("Extensi on : %s\n", ext);
    }
    else
    printf ("sscanf_s failed\n");
    }
    }

    And I got a compile-time error message saying "error C2664 : 'sscanf_s' cannot convert parameter 1 from 'WCHAR [260]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cas t, C-stype cast or function-style cast.

    Could anyone please help me fix this compile-time error?

    Thank you very much for your help.

    Akino
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    The variable cFileName is of type TCHAR.
    This is what i see in the TCHAR definition when searching in MSDN.
    Code:
    #ifdef Unicode
    typedef WCHAR TCHAR;
    #else
    typedef CHAR TCHAR;
    #endif
    Since the WCHAR is used you cant typecast it to char*.
    So dont define Unicode and after that u can convert to char*

    Raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You can't use sscanf(). Nor can you use char or char*:
      Originally posted by Akino877
      char filename[128]; <--- ERROR
      char ext[128]; <--- ERROR
      You have to use TCHAR.
      [code=cpp]
      TCHAR filename[128];
      TCHAR ext[128];
      [/code]


      sscanf() only works with char. When Unicode is defined it has to work with wchar_t, which is WCHAR. TCHAR maps between char and wchar_t based on the Unicode settin gin your build.

      The TCHAR compatible sscanf() is _stscanf().

      Read this: http://msdn.microsoft.com/en-us/library/ms860358.aspx.

      Your code should be:
      [code=cpp]
      if (_stscanf_s (ffd.cFileName, TEXT("%16s.%3s" , filename, ext) == 2)
      {
      [/code]

      And lastly, the printf() have to be got rid of:
      [code=cpp]
      _tprintf(TEXT(" File Name : %s\n"), filename);
      _tprintf(TEXT(" Extension : %s\n"), ext);
      etc...
      [/code]

      Remember, with TCHAR you cannot ever use the native type char or wchat_t.

      Comment

      Working...