在视觉工作室中出现错误:错误C2664:' _chmod' :无法转换参数1来自' wchar_t [260]'到#char; char *'

时间:2017-02-10 10:48:02

标签: c++ visual-studio visual-studio-2012 delete-file msdn

删除任何文件夹的C ++代码:

#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

希望任何人都可以帮助!!

我收到以下错误:

  

错误C2664:&#39; _chmod&#39; :无法从&#39; wchar_t转换参数1   [260]&#39; to&#39; const char *&#39;

P.S。我使用Microsoft Visual Studio。

1 个答案:

答案 0 :(得分:2)

您正在使用宽字符,但调用该函数的窄字符版本。

由于设置了UNICODE宏,大多数Windows API函数transparently switch between the two。 &#34; chmod&#34;具有此行为的函数是_tchmod

您应切换为_tchmod