问题出在哪里以及为什么程序在运行时崩溃并在调试时失败? [英] Where is the problem and why does the program crash on run and fail on debug?

查看:90
本文介绍了问题出在哪里以及为什么程序在运行时崩溃并在调试时失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:运行时崩溃(程序已停止工作)和调试时的SIGSEGV(程序接收信号SIGSEGV,分段故障)

该程序可用于根据LastWriteTime对文件进行排序和复制到不同的目录(年份和季度)。

Errors: Crash on run ("Program has stopped working") and the SIGSEGV ("Program received signal SIGSEGV, Segmentation fault") on debug
The program can be used to sort and copy files into different directories (years and quarters) according to their LastWriteTime.

void sort(std::string quartal, char * c_pfad, SYSTEMTIME datum){
            year = datum.wYear;
            str_year =  patch::to_string(year);             // convert

            year_path = dir + str_year + "\\";  // C:\\Pictures\\2015\\

            CreateDirectory(convert(year_path), NULL);                              // year dir

            quartal_path = year_path + quartal; // C:\\Pictures\\2015\\Erstes Quartal\\

            CreateDirectory(convert(quartal_path), NULL);                               // quarter dir

            ziel = quartal_path + wfd.cFileName;

            CopyFile( c_pfad, convert(ziel), false ); 

}

void schleife(HANDLE fHandle){


do 
{ 
    // not . or ..  (subdirectories) 
    if (!( (wfd.cFileName[0]=='.') && ( (wfd.cFileName[1]=='.' && wfd.cFileName[2]==0) || wfd.cFileName[1]==0 ) )) 
    { 
        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
        { 
              fHandle = fHandleBef; // Probably the problem is here
              findDirGes = findDirGes.substr (0, findDirGes.size()-1) + wfd.cFileName + "\\*";
              schleife(FindFirstFile(convert(findDirGes),&wfd));            // Not a file but a directory 
              fHandle = fHandleBef; // Probably the problem is here                                                             
        } 
        else 
        { 
           pfad = findDirGes.substr (0, findDirGes.size()-1) + wfd.cFileName;



          WIN32_FILE_ATTRIBUTE_DATA dateidaten; 

          GetFileAttributesEx(convert(pfad), GetFileExInfoStandard, &dateidaten); 
          // cprintf("\n %d", dateidaten.ftLastWriteTime); 

          FILETIME test = dateidaten.ftLastWriteTime; 
          // cprintf("\n %d", test); 
          SYSTEMTIME datum; 

          FileTimeToSystemTime(&test, &datum);


        switch (datum.wMonth){
        case 1:
        case 2:
        case 3:
            sort("Erstes Quartal\\", convert(pfad), datum );
            break;
        case 4:
        case 5:
        case 6:
            sort("Zweites Quartal\\", convert(pfad), datum );
            break;
        case 7:
        case 8:
        case 9:
            sort("Drittes Quartal\\", convert(pfad), datum );
            break;
        case 10:
        case 11:
        case 12:
            sort("Viertes Quartal\\", convert(pfad), datum );
            break;
  }

        } 
    } 
} 
while (FindNextFile(fHandle,&wfd)); 
FindClose(fHandle);

}




int main() { 


std::cout << "Hier den Ordner mit unsortierten Dateien eingeben ( auf Doppelbackslash achten ! ) :" << std::endl;
std::cin >> ordner;

std::cout << "Hier den Zielordner angeben ( auf Doppelbackslash achten ! ):" << std::endl;
std::cin >> dir;                                                                                                    // User-Interface

findDirGes = ordner + "*";

schleife(FindFirstFile(convert(findDirGes),&wfd));

}



该程序的问题在于它遍历您输入的目录中的所有文件,如果它来了它到另一个目录,它完美地排序和复制文件,但之后它不会继续上面目录中的文件(或目录)。行fHandle = fHandleBef解决了这个问题,但产生了所描述的错误。



所以基本上程序工作正常,但在调试和运行时返回错误。



感谢您的帮助。

推荐答案

问题是您在递归中使用全局变量功能。找到第一个目录后,将其名称复制到 findDirGes 并处理该目录。从该递归函数调用返回时, findDirGes 包含该子目录的掩码,而不是用于解析初始目录的掩码。



解决方案是将目录名传递给递归函数,并使用局部变量在函数内创建掩码。还要在递归函数中调用FindFirstFile来关闭实际查找循环的句柄:

The problem is that you are using global variables within a recursive function. When the first directory is found, it's name is copied to findDirGes and the directory is processed. When returning from that recursive function call, findDirGes contains the mask of that sub directory and not the mask for parsing the initial directory.

The solution is to pass the directory name to the recursive function and create the mask inside the function using a local variable. Also call FindFirstFile inside the recursive function to close the handle for the actual find loop:
void schleife(const std::string& path)
{
    std::string mask = path + "\\*";
    LPWIN32_FIND_DATA wfd;
    HANDLE fHandle = FindFirstFile(convert(mask), &wfd);
    if (INVALID_HANDLE_VALUE != fHandle)
    {
        do {
            // Process files here
            // For a directory, create the path using a new local variable:
            // std::string subpath = path + '\\' + wfd.cFileName;
            // schleife(subpath);
        } while FindNextFile(fHandle, &wfd));
        FindClose(fHandle);
    }
}



请注意,我还将 wfd 声明为本地。这可确保在处理子目录后它仍然有效。


Note that I have also declared wfd as local. This ensures that it is also still valid after processing a sub directory.


这篇关于问题出在哪里以及为什么程序在运行时崩溃并在调试时失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆