c++ - 应用程序首先崩溃 strcat_s

标签 c++ file search crash strcat-s

我已经尝试过 strcat 和 strcat_s,但它们都崩溃了。有谁知道为什么会这样?我找不到问题。

Crash: "Unhandled exception at 0x58636D2A (msvcr110d.dll)"
_Dst        0x00ea6b30  "C:\\Users\\Ruben\\Documents\\School\\" char *
_SizeInBytes    260                         unsigned int
_Src        0x0032ef64  "CKV"                   const char *
available       228                         unsigned int
p           0x00ea6b50  ""                  char *

代码:

#include <Windows.h>
#include <strsafe.h>

extern "C"
{
    char* GetFilesInFolders(LPCWSTR filedir, char* path)
    {
        char* files = "";

        char DefChar = ' ';
        char* Streepje = "-";
        bool LastPoint = false;

        WIN32_FIND_DATA ffd;
        TCHAR szDir[MAX_PATH];
        HANDLE hFind = INVALID_HANDLE_VALUE;
        DWORD dwError = 0;

        StringCchCopy(szDir, MAX_PATH, filedir);
        hFind = FindFirstFile(szDir, &ffd);

        if (INVALID_HANDLE_VALUE == hFind) 
            return "";

        do
        {
            DWORD attributes = ffd.dwFileAttributes;
            LPCWSTR nm = ffd.cFileName;
            char name[260];
            WideCharToMultiByte(CP_ACP,0,ffd.cFileName,-1, name,260,&DefChar, NULL);

            for (int i = 0; i <= 260; i++)
            {
                if (name[i] == '.')
                    LastPoint = true;
                else if (name[i] == ' ')
                    break;
            }

            if (LastPoint == true)
            {
                LastPoint = false;
                continue;
            }

            if (attributes & FILE_ATTRIBUTE_HIDDEN)
            {
                continue;
            }
            else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                char* newfiledir = "";
                char* newpath = path;
                char* add = "\\";
                char* extra = "*";
                strcat_s(newpath, sizeof(name), name);
                strcat_s(newpath, sizeof(add), add);
                puts(newpath);
                strcpy_s(newfiledir, sizeof(newpath) + 1, newpath);
                strcat_s(newfiledir, sizeof(extra) + 1, extra);
                puts(newfiledir);

                size_t origsize = strlen(newfiledir) + 1;
                const size_t newsize = 100;
                size_t convertedChars = 0;
                wchar_t wcstring[newsize];
                mbstowcs_s(&convertedChars, wcstring, origsize, newfiledir, _TRUNCATE);
                LPCWSTR dir = wcstring;
                GetFilesInFolders(dir, newpath);
            }
            else
            {
                char* file = path;
                strcat_s(file, sizeof(name), name);
                puts(file);
                strcat_s(files, sizeof(file), file);
                strcat_s(files, sizeof(Streepje), Streepje);
                puts(files);
            }
        }
        while (FindNextFile(hFind, &ffd) != 0);

        FindClose(hFind);
        return files;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    char* path = "C:\\Users\\Ruben\\Documents\\School\\";
    char* filedir = "C:\\Users\\Ruben\\Documents\\School\\*";
    size_t origsize = strlen(filedir) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    wchar_t wcstring[newsize];
    mbstowcs_s(&convertedChars, wcstring, origsize, filedir, _TRUNCATE);
    LPCWSTR dir = wcstring;
    char* files = GetFilesInFolders(dir, path);
    return 0;
}

额外信息:我不想使用 boost 或字符串,我想将其保留为 unicode(默认)。

最佳答案

您将 const char* 分配给 files,然后尝试附加到它。

char* files = "";
// ... 
strcat_s(files, sizeof(file), file);

您不能修改常量字符串文字。

我建议您打开编译器警告并确保查看它们。这将警告您将 const char* 分配给 char*。要修复它,您可能已将 files 更改为 const,这将导致您的 strcpy_s 不再编译。

关于c++ - 应用程序首先崩溃 strcat_s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12587086/

相关文章:

c++ - OpenCV:使用霍夫圆变换检测虹膜

c++ - 如何在OpenCV中使用Matlab的512元素查找表数组?

c# - 使用我的 C# 应用程序打开特定文件——例如使用 Word 打开 .doc 文件

c - 在 block 中读取 2 字节长的整数

python - 二进制搜索 : weird middle point calculation

c++ - 进程列表及其子进程

c++ - 如何将整数转换为字符串

java - 从服务器下载文件

algorithm - 顺序搜索时间中的预期搜索时间

python - 搜索 csv 文件,这是最佳实践?