如何获取临时文件夹并设置临时文件路径?
问题描述:
如何获取临时文件夹并设置临时文件路径?我尝试了代码波纹管,但它有错误。非常感谢你!如何获取临时文件夹并设置临时文件路径?
TCHAR temp_folder [255];
GetTempPath(255, temp_folder);
LPSTR temp_file = temp_folder + "temp1.txt";
//Error: IntelliSense: expression must have integral or unscoped enum type
答
此代码添加了两个指针。
LPSTR temp_file = temp_folder + "temp1.txt";
这不concatenating字符串,它不是创造你想要的结果字符串的任何存储。
TCHAR temp_file[255+9]; // Storage for the new string
lstrcpy(temp_file, temp_folder); // Copies temp_folder
lstrcat(temp_file, T("temp1.txt")); // Concatenates "temp1.txt" to the end
基于the documentation for GetTempPath
,这也将是明智的MAX_PATH+1
取代的255
所有出现在你的代码。
答
您不能将两个字符数组一起添加并获得有意义的结果。它们是指针,而不是像std :: string这样的提供这种有用操作的类。
创建一个足够大的TCHAR数组并使用GetTempPath,然后使用strcat为其添加文件名。
TCHAR temp_file [265];
GetTempPath(255, temp_file);
strcat(temp_file, "temp1.txt");
理想情况下,您还应该测试GetTempPath的失败结果。就我从另一个答案中链接的文档中可以看出,失败的最可能原因是提供的路径变量太小。按照推荐的那样使用MAX_PATH + 1 + 9。
关于使用MAX_PATH + 1的好处 – Muscles 2013-04-24 03:53:02