【发布时间】:2018-08-23 21:21:26
【问题描述】:
您好,我正在使用 Visual Studio 并尝试制作一个将自身复制到磁盘的程序,当我运行它时,它就是这样做的,但随后我收到消息:
"*Run-Time Check Failure #2 - Stack around the variable 'folderPath' was corrupted*."
代码如下:
void copyToDrive(char driveLetter) {
char folderPath[10] = { driveLetter };
strcat(folderPath, ":\\");
strcat(folderPath, FILE_NAME);
char filename[MAX_PATH];
DWORD size = GetModuleFileNameA(NULL, filename, MAX_PATH);
std::ifstream src(filename, std::ios::binary);
std::ofstream dest(folderPath, std::ios::binary);
dest << src.rdbuf();
return;
}
是什么原因造成的?我该如何解决?
【问题讨论】:
-
您放入
folderPath的字符串从不会超过九个字符吗?也就是说,FILE_NAME的长度永远不会超过六个字符吗? -
顺便问一下
FILE_NAME是什么是?请read about how to ask good questions,并学习如何创建Minimal, Complete, and Verifiable Example。 -
#define FILE_NAME "app.exe" 和 folderPath 是磁盘的地址:将类似于 d:\\app.exe
-
您的
folderPath数组太小。c:\app.exe是 10 个字符,您还需要一个用于 nul 终止符。 -
没有必要将答案编辑到问题中,问题应该只包含问题而不是答案。如果您对自己的问题有自己的答案,请将其添加为答案
标签: c++