GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'

Sneh Shikhar

C++ code to delete any folder:

#include <string>
#include <iostream>
#include "stdafx.h"
#include <stdio.h>
#include <afx.h>
#include <windows.h>
#include <conio.h>
#include <io.h>

using namespace std;

BOOL IsDots(wchar_t* str)
{
    if (_tcscmp(str, TEXT(".")) && _tcscmp(str, TEXT("..")))
        return FALSE;
    return TRUE;
}

BOOL DeleteDirectory(wchar_t* sPath)
{
    HANDLE hFind;
    WIN32_FIND_DATA FindFileData;

    wchar_t DirPath[MAX_PATH];
    wchar_t FileName[MAX_PATH];

    _tcscpy(DirPath, sPath);
    _tcscat(DirPath, TEXT("\\*"));
    _tcscpy(FileName, sPath);
    _tcscat(FileName, TEXT("\\"));

    //GETTING THE FISRT FILE

    hFind = FindFirstFile(DirPath, &FindFileData);
    if (hFind == INVALID_HANDLE_VALUE)
        return FALSE;
    _tcscpy(DirPath, FileName);

    bool bSearch = true;
    while (bSearch) {
        if (FindNextFile(hFind, &FindFileData)) {

            if (IsDots(FindFileData.cFileName))
                continue;

            _tcscat(FileName, FindFileData.cFileName);
            if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {

                //DELETING THE DIRECTORY

                if (!DeleteDirectory(FileName)) {
                    FindClose(hFind);
                    return FALSE;
                }
                RemoveDirectory(FileName);
                _tcscpy(FileName, DirPath);
            }

            else {

                if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
                    _chmod(FileName, _S_IWRITE);
                mode if (!DeleteFile(FileName))
                {
                    FindClose(hFind);
                    return FALSE;
                }

                _tcscpy(FileName, DirPath);
            }
        }

        else {
            if (GetLastError() == ERROR_NO_MORE_FILES)
                bSearch = false;
            else {

                FindClose(hFind);
                return FALSE;
            }
        }
    }
    FindClose(hFind);

    return RemoveDirectory(sPath);
}

//CALLING THE DEL DIR FUNCTION

HOPE ANYONE COULD HELP!!

I get the following error:

error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'

P.S. I use Microsoft Visual Studio.

Lightness Races in Orbit

You're using wide characters, but calling the narrow character version of the function.

Most Windows API functions transparently switch between the two by virtue of the UNICODE macro being set. The "chmod" function with this behaviour is _tchmod.

You should switch to _tchmod.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C++ --- error C2664: 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'

error C2664: 'LoadLibraryA' : cannot convert parameter 1 from 'WCHAR *' to 'LPCSTR'

error C2664: 'errno_t strcpy_s(char *,rsize_t,const char *)' : cannot convert parameter 2 from 'char [512]' to 'rsize_t'

Error C2664: 'Strategy::Interphase' : cannot convert parameter 2 from 'char *' to 'char *[]'

Error C2664: MessageBoxW cannot convert argument 2 from 'const char ' to 'LPCWSTR'

Error C2664 'HRESULT IUnknown::QueryInterface(const IID &,void **)': cannot convert argument 1 from 'const winrt::guid' to 'const IID &'

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

error C2664: 'print_result' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,int)' to 'int'

C2664 Cannot convert parameter 1 from 'int' to 'hwnd'

cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

c++ doubly linked list error C2664 'object1::object1(object1 &&)': cannot convert argument 1 from 'int' to 'const object1 &'

C++ c2664 error "cannot convert argument 1 from std::string to _Elem *"

Visual Studio OpenCV error C2664

Cannot convert argument from wchar_t[260] to LPSTR

strcmp error 'WCHAR [260]' to 'const char *'

cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'

error C2664: cannot convert 'IDWriteFactory2 **' to 'IUnknown **'

Error C2664 'BOOL CryptBinaryToStringW(const BYTE *,DWORD,DWORD,LPWSTR,DWORD *)': cannot convert argument 4 from 'std::unique_ptr' to 'LPWSTR'

error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR'

Cannot convert parameter 1 from char to const char for strcmp

Convert const char* to const wchar_t*

win32 atoi issue which can't convert argument 1 from 'wchar_t [5]' to 'const char *

Error: argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *"

"cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc" when trying to use WriteConsoleOutputCharacter()

String parameter const char* and const wchar_t*

Visual Studio 2017; argument of type "const wchar_t *" is incompatible with parameter of type "PWSTR" when /permissive is enabled

error: cannot convert 'const char**' to 'const char (*)[64]'

error C2440: 'return' : cannot convert from 'char *const ' to 'LPCTSTR'