gpt4 book ai didi

c++ - 为什么这个字符串被神秘地修改了?

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:20 24 4
gpt4 key购买 nike

如果这个问题的答案很简单,请原谅我,但我不知道是什么原因造成的。 PathCombineA 函数以某种方式修改了 mypath 变量。如果你运行这个程序,你就会明白我的意思。 (必须链接 Shlwapi.lib)

#include <Windows.h>
#include <Shlwapi.h>
#include <iostream>

using namespace std;

int main()
{
CHAR temp[MAX_PATH];
CHAR mypath[MAX_PATH];

GetModuleFileNameA(NULL, mypath, MAX_PATH);
GetTempPathA(MAX_PATH, temp);

LPSTR name = PathFindFileNameA(mypath);

cout << mypath << endl;

PathCombineA(name, temp, name);

cout << mypath << endl;

getchar();
return 0;
}

PathCombineA 之前的输出

C:\Users\Owner\Desktop\etc\Debug\etc.exe


PathCombineA 之后的输出

C:\Users\Owner\Desktop\etc\Debug\C:\Users\Owner\AppData\Local\Temp\etc.exe


如果你们知道发生了什么,请告诉我!

谢谢!

最佳答案

PathFindFileNameA 返回指向 mypath 中字符串最后部分的指针。然后将该指针作为 PathCombineA 的输出参数传递到 mystring 缓冲区。

如果您不想修改 mystring,您将需要另一个缓冲区来保存 PathCombineA 的输出。

关于c++ - 为什么这个字符串被神秘地修改了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20589595/

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