gpt4 book ai didi

c - 将字符串添加到 LPTSTR

转载 作者:行者123 更新时间:2023-11-30 19:12:20 25 4
gpt4 key购买 nike

我想向 LPTSTR 添加一个字符串。

代码是:

hSourceFile = CreateFile(
pszSourceFile,
FILE_READ_DATA,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE != hSourceFile)
{
_tprintf(
TEXT("The source plaintext file, %s, is open. \n"),
pszSourceFile);
}

pszSourceFile 是一种 LPTSTR,但我想添加一些额外的文本。

喜欢(不起作用)

pszSourceFile + ".txt"

最好的方法是什么?

最佳答案

考虑 C 风格和 Windows API 的使用(与 TEXT() 等);使用 _tcscat() _tcsncat() (后者需要缓冲区大小)。

例如;

TCHAR buffer[1024] = {}; // or '\0'
_tcsncat(buffer, pszSourceFile, 1024);
_tcsncat(buffer, TEXT(".txt"), 1024);

Demo .

警告;注意缓冲区溢出。假设“正常”Windows 260 字符路径文件和名称限制 ( _MAX_PATH ),缓冲区需要满足这一点。

对于 C++(如最初标记的那样),另一种方法是使用 std::basic_string<TCHAR>然后是 operator+ (或 += )照常。 .c_str()将为您提供结果字符串;

std::basic_string<TCHAR> buffer(pszSourceFile);
buffer += TEXT(".txt");
auto ptr = buffer.c_str();

关于c - 将字符串添加到 LPTSTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37277166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com