将 char 数组连接在一起
Concatenating char arrays together
几年前我会认为这是微不足道的事情...自从我涉足 C 或 C++ 以来已经有一段时间了,我遇到了一个现在导致偏头痛的问题。
我收到以下代码的错误消息:
CompressFile::CompressFile(wchar_t *WorkingDirectory, wchar_t *File)
{
int bzipError = BZ_OK;
wprintf(L"Attempting to compress %s%s\n", WorkingDirectory, File);
wchar_t FileRawInput[MAX_PATH];
wcsncpy(FileRawInput, WorkingDirectory, sizeof(FileRawInput));
wcsncat(FileRawInput, File, sizeof(FileRawInput));
wchar_t bzipCompressedOutput[MAX_PATH];
wcsncpy(bzipCompressedOutput, FileRawInput, sizeof(bzipCompressedOutput));
wcscat(bzipCompressedOutput, L".bz2");
wprintf(L"Output of string bzip: %s\n", bzipCompressedOutput);
wprintf(L"Output of string raw: %s\n", FileRawInput);
}
我在第 8 行收到以下错误:
Unhandled exception at 0x64F4C6D1 in ias-agent.exe: 0xC00001A5: An invalid exception handler routine has been detected (parameters: 0x00000003).
我已经竭尽全力避免使用 string
class,我想暂时保持这种状态。我想要做的就是将两个字符串加在一起以获得 RawFileInput
,然后将 RawFileInput
的值添加到 bzipCompressionOutput
,最后,将 .bz2
连接到 [= 的末尾15=].
在last page of chapter 4 in his book: "The C++ Programming Language" Bjarne Stroustrup the creator of C++中说:
Prefer strings
over C-style strings
这只是建议,但我鼓励您遵循它。
但你真正的问题是你在踩内存 not sizeof(FileRawInput)
wchar_t
s in your FileRawInput
同样有bzipCompressedOutput
数组中没有 sizeof(bzipCompressedOutput)
,两个数组中都有 MAX_PATH
wchar_t
。问题是 sizeof
会告诉你数组中的字节数,但如果每个元素都大于 1 个字节,那么你就错误地告诉了 wcsncpy
和 wscncat
你的字符数。 wchar_t
通常是 2 个字节:https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx 意味着你正在有效地调用 wcsncpy(FileRawInput, WorkingDirectory, 200)
。超出您分配的内存 100 wchar_t
s。更正此问题将消除您的段错误。
但是为了打印宽字符串,您需要正确使用 %ls
修饰符来 wprintf
。
最终您的代码应如下所示:
wprintf(L"Attempting to compress %ls%ls\n", WorkingDirectory, File);
wchar_t FileRawInput[MAX_PATH];
wcsncpy(FileRawInput, WorkingDirectory, MAX_PATH);
wcsncat(FileRawInput, File, MAX_PATH);
wchar_t bzipCompressedOutput[MAX_PATH];
wcsncpy(bzipCompressedOutput, FileRawInput, MAX_PATH);
wcscat(bzipCompressedOutput, L".bz2");
wprintf(L"Output of string bzip: %ls\n", bzipCompressedOutput);
wprintf(L"Output of string raw: %ls\n", FileRawInput);
编辑:
OP 已默认 Bjarne Stroustrup 的建议并转至 wstring
: 但是对于仍然坚持使用这些 C 风格函数的其他任何人,MAX_PATH
必须是足够大以容纳 wsclen(WorkingDirectory) + wsclen(File) + wsclen(L".bz2")
加上 L'[=31=]'
字符,因此在此函数上放置一个 if 语句可能会有用,或者也许:
assert(MAX_PATH > wsclen(WorkingDirectory) + wsclen(File) + wsclen(L".bz2"))
几年前我会认为这是微不足道的事情...自从我涉足 C 或 C++ 以来已经有一段时间了,我遇到了一个现在导致偏头痛的问题。
我收到以下代码的错误消息:
CompressFile::CompressFile(wchar_t *WorkingDirectory, wchar_t *File)
{
int bzipError = BZ_OK;
wprintf(L"Attempting to compress %s%s\n", WorkingDirectory, File);
wchar_t FileRawInput[MAX_PATH];
wcsncpy(FileRawInput, WorkingDirectory, sizeof(FileRawInput));
wcsncat(FileRawInput, File, sizeof(FileRawInput));
wchar_t bzipCompressedOutput[MAX_PATH];
wcsncpy(bzipCompressedOutput, FileRawInput, sizeof(bzipCompressedOutput));
wcscat(bzipCompressedOutput, L".bz2");
wprintf(L"Output of string bzip: %s\n", bzipCompressedOutput);
wprintf(L"Output of string raw: %s\n", FileRawInput);
}
我在第 8 行收到以下错误:
Unhandled exception at 0x64F4C6D1 in ias-agent.exe: 0xC00001A5: An invalid exception handler routine has been detected (parameters: 0x00000003).
我已经竭尽全力避免使用 string
class,我想暂时保持这种状态。我想要做的就是将两个字符串加在一起以获得 RawFileInput
,然后将 RawFileInput
的值添加到 bzipCompressionOutput
,最后,将 .bz2
连接到 [= 的末尾15=].
在last page of chapter 4 in his book: "The C++ Programming Language" Bjarne Stroustrup the creator of C++中说:
Prefer
strings
over C-style strings
这只是建议,但我鼓励您遵循它。
但你真正的问题是你在踩内存 not sizeof(FileRawInput)
wchar_t
s in your FileRawInput
同样有bzipCompressedOutput
数组中没有 sizeof(bzipCompressedOutput)
,两个数组中都有 MAX_PATH
wchar_t
。问题是 sizeof
会告诉你数组中的字节数,但如果每个元素都大于 1 个字节,那么你就错误地告诉了 wcsncpy
和 wscncat
你的字符数。 wchar_t
通常是 2 个字节:https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx 意味着你正在有效地调用 wcsncpy(FileRawInput, WorkingDirectory, 200)
。超出您分配的内存 100 wchar_t
s。更正此问题将消除您的段错误。
但是为了打印宽字符串,您需要正确使用 %ls
修饰符来 wprintf
。
最终您的代码应如下所示:
wprintf(L"Attempting to compress %ls%ls\n", WorkingDirectory, File);
wchar_t FileRawInput[MAX_PATH];
wcsncpy(FileRawInput, WorkingDirectory, MAX_PATH);
wcsncat(FileRawInput, File, MAX_PATH);
wchar_t bzipCompressedOutput[MAX_PATH];
wcsncpy(bzipCompressedOutput, FileRawInput, MAX_PATH);
wcscat(bzipCompressedOutput, L".bz2");
wprintf(L"Output of string bzip: %ls\n", bzipCompressedOutput);
wprintf(L"Output of string raw: %ls\n", FileRawInput);
编辑:
OP 已默认 Bjarne Stroustrup 的建议并转至 wstring
:MAX_PATH
必须是足够大以容纳 wsclen(WorkingDirectory) + wsclen(File) + wsclen(L".bz2")
加上 L'[=31=]'
字符,因此在此函数上放置一个 if 语句可能会有用,或者也许:
assert(MAX_PATH > wsclen(WorkingDirectory) + wsclen(File) + wsclen(L".bz2"))