【问题标题】:GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *' [closed]在 VISUAL STUDIO 中出现错误:错误 C2664:“_chmod”:无法将参数 1 从“wchar_t [260]”转换为“const char *”[关闭]
【发布时间】:2017-06-28 16:32:10
【问题描述】:

删除任何文件夹的 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:“_chmod”:无法从“wchar_t”转换参数 1 [260]' 到 'const char *'

附:我使用 Microsoft Visual Studio。

【问题讨论】:

  • 我认为错误非常明显。您有一个wchar_t 数组并尝试调用一个需要char 数组的函数。这可能是由于您的UNICODE 宏设置不匹配,而_chmod 是一个非标准函数,不提供宽字符和窄字符替代方案。尝试改用 Windows API 函数,或 _wchmod
  • 请不要再喊了。
  • _wchmod 工作了很多 :)
  • @Someprogrammerdude:请在答案部分回答,不要在 cmets 中回答。

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


【解决方案1】:

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

大多数 Windows API 函数 transparently switch between the two 是由于设置了 UNICODE 宏。具有这种行为的“chmod”函数是_tchmod

你应该切换到_tchmod

【讨论】:

  • 我强烈建议阅读我链接到的文档页面。
猜你喜欢
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 2021-03-01
  • 1970-01-01
相关资源
最近更新 更多