2012-09-25 253 views
0

我已經嘗試了strcat和strcat_s,但它們都崩潰。有誰知道爲什麼發生這種情況?我找不到問題。第一次應用程序崩潰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; 
} 

額外的信息:我不想使用升壓或字符串,我想保持這種以Unicode(默認)。

+0

是空名稱???你有沒有嘗試valgrind? – 2012-09-25 16:23:48

+0

「名稱」不爲空。 – para

+1

既然'add'是一個char *',所以sizeof(add)'和sizeof(char *)'是一樣的,這絕對不是你想要的。說白了,看起來你根本不懂如何使用C風格的字符串。 –

回答

1

看起來你不明白變量是如何存儲在內存中的,或者指針是如何工作的。在您的_tmain()中,您有char * path指向一個常量字符串文字,您將其傳遞到GetFilesInFolders(),它在那裏被修改。編譯器傾向於允許char *指向不變的字符串,以便與舊C程序向後兼容。你不能修改這些。你不能追加到他們。編譯器(通常)將它們放在只讀段中。這是你獲得例外的原因之一。

您的整個GetFilesInFolders()是錯誤的。正如DarkFalcon指出的那樣,您沒有在任何地方爲files分配任何空間,您將它指向一個常量字符串文字。

獲取「The C++ Programming Language」並閱讀第5章。

2

您將const char*指定爲files,然後嘗試附加到它。

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

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

我建議你打開編譯器警告,並確保看看它們。這會警告您將const char*分配給char*。要修復它,您可能已將files更改爲const,這會導致您的strcpy_s不再編譯。

+0

目標是在只讀內存中的好處。目標緩衝區的大小不足也是一個嚴重的問題。 –

+0

我有編譯警告打開AFAIK,我沒有得到任何錯誤消息。 「文件」和「文件」都是char *。 – para

相關問題