我该如何处理无效的异常处理程序例程?

时间:2020-04-13 15:13:33

标签: c++ exception visual-c++

我正在构建一个程序,该程序创建和删除目录。我使用MSVC编译器(Visual Studio 2017),这就是我不能分别使用“ getcwd()”或“ dirent.h”的原因。 我尝试了几种不同的方法来获取当前的工作目录,但始终存在问题。我设法用“ _wgetcwd()”打印cwd。但是,在我的研究中,我找不到如何转换它的输出以在“ _rmdir()”或“ _wrmdir()”中使用它的方法。

我的主要目标是能够删除目录而无需安装一些新的编译器。如果满足此条件,将不胜感激,因为我已经尝试安装其他Compiler,但没有使它起作用。我还尝试了不同的方法来获取cwd并将其转换为所需的数据类型,但是在我的IDE范围和我的基本知识中没有任何作用。

我几乎是编程的新手,我正在学习这本书,不幸的是使用了“ dirent.h”。以下是我当前代码的片段,其中我已消除了所有错误。但是我仍然得到最后一个令人讨厌的异常:

#include <iostream>
#include <stdlib.h>
#include<string>
#include <direct.h>

int main() {

    int t = 0;
    std::string str;
    char xy = ' ';
    char* _DstBuf = &xy;
    const char* uniquename;
    uniquename = _getdcwd(0, _DstBuf, _MAX_PATH);
    std::cout << "Current path is ";    
    while (*uniquename != '\0') // this pointer points to the beginning of my path
    {
        std::cout << char(*uniquename); //  prints one char of the path name for each iteration

        char var = char(*uniquename);

        str.push_back(var); //here the exception below is thrown.

        // jump to the next block of memory
        uniquename++;
    }
    std::cout << str <<'\n';

    const char* lastchance = str.c_str();

    if (_wchdir('..')) {
        std::cout << "changing directory failed.\n";
    }

    if (_rmdir(lastchance) == -1 && errno == ENOTEMPTY) { 
        std::cout << "Given path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory.\n";
    }
    else if (_rmdir(lastchance) == -1 && errno == ENOENT) 
    {
        std::cout << "Path is invalid.\n";
    }
    else if (_rmdir(lastchance) == -1 && errno == EACCES)
    {
        std::cout << "A program has an open handle to the directory.\n";
    }
    else if (_rmdir(lastchance)) {
        std::cout << "removing directory still not possible\n";
    }

}

这是我得到的例外: Experimentfile.exe中0x6E656D69处未处理的异常:0xC00001A5:检测到无效的异常处理程序例程(参数:0x00000003)。

2 个答案:

答案 0 :(得分:3)

因此,如果要使用C风格进行编程(即使您具有C ++编译器),则必须学习数组和指针的工作方式。 的主题太大,无法从互联网上学习,因此需要good book

但是我会指出一些错误

char xy = ' ';
char* _DstBuf = &xy;
const char* uniquename;
uniquename = _getdcwd(0, _DstBuf, _MAX_PATH);

这是无可救药的错误。它可以编译,但这并不意味着它将起作用。这是必需的

char DstBuf[_MAX_PATH];
_getdcwd(0, DstBuf, _MAX_PATH);

_getdcwd需要一个 array 作为指针传递(请参阅,您需要了解数组和指针)。

然后,您尝试打印结果并将结果分配给字符串。同样,代码比所需的复杂得多。这是更简单的版本

std::string str = DstBuf;
std::cout << str <<'\n';

然后尝试更改目录。我不知道为什么在使用窄字符串时为什么使用宽版本_wchdir,请改用_chdir。同样,该参数不正确,'..'是多字符文字,但是_chdir需要C字符串。这是使用_chdir的正确版本。

if (_chdir("..")) {
    std::cout << "changing directory failed.\n";
} 

然后您尝试删除一个目录四次,显然您不能多次删除一个目录。

等等,依此类推。

答案 1 :(得分:2)

您的代码存在几个问题。我认为您的例外情况是由以下行引起的:

uniquename = _getdcwd(0, _DstBuf, _MAX_PATH);

正在调用_getdcwd并向其传递指向单个字符的指针,同时还指定该指针最多可容纳_MAX_PATH。这是完全不确定的行为。

要解决此问题,我认为您应该完全改变自己的工作方式。除了分散的C ++部分(例如std::string)之外,您的代码读起来像C代码。自C ++ 17起,标准库就内置了对文件系统操作的支持。因此,您可以使用std::filesystem以更加简洁和安全的方式重写代码。

... // get path to delete and store it in del_path (del_path should be an std::filesystem::path object)
std::filesystem::remove_directories(del_path); // Recursive delete the directory specified by del_path
...

注意:std::filesystem::remove_directories如果失败将抛出std::filesystem::filesystem_error

您可以使用std::filesystemstd::filesystem::current_path获取当前目录。

我还建议您在C ++上学习better book,因为这似乎并没有教给您良好的做法

相关问题