Domanda

How to get temp folder and set a temp file path? I tried code bellow but it have error. Thank you very much!

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
È stato utile?

Soluzione

This code is adding two pointers.

LPSTR temp_file = temp_folder + "temp1.txt";

It's not concatenating the strings and it's not creating any storage for the resultant string you want.

For C-style strings, use lstrcpy and lstrcat

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

Based on the documentation for GetTempPath, it would also be wise to replace all occurances of 255 in your code with MAX_PATH+1.

Altri suggerimenti

You can't add two character arrays together and get a meaningful result. They're pointers, not a class like std::string which provides such useful operations.

Create a large enough TCHAR array and use GetTempPath, then use strcat to add the file name to it.

TCHAR temp_file [265];
GetTempPath(255, temp_file);
strcat(temp_file, "temp1.txt");

Ideally, you should also test the result of GetTempPath for failure. As far as I can see from the documentation linked in the other answer, the most likely reason for failure is that the path variable supplied is too small. Use MAX_PATH+1+9 as recommended there.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top