views:

62

answers:

4

Hi,

I would like to append two strings together so that I can rename a file using the MoveFile function. But my strings refuse to concatenate, so instead of adding "E:\" to "FILE-%s-%02d%02d%02d-%02d%02d.txt" to give me "E:\FILE-%s-%02d%02d%02d-%02d%02d.txt", it gives me just "E:\" as if nothing happened.

Here is a snippet of my full code:

  drivePathAgain = "E:\\";

  sprintf(newname, "FILE-%s-%02d%02d%02d-%02d%02d.txt",  szVolNameBuff, lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute);
  lstrcat((LPWSTR)drivePathAgain, (LPWSTR)newname);
  result = MoveFile((LPCWSTR) drivePath, (LPCWSTR) drivePathAgain );

I can't append newname to drivePathAgain. If you need me to post the entire code to get the big picture, I can. Is there a way to append strings like that?

Thanks

+2  A: 

This statement:

drivePathAgain = "E:\\";

suggests that drivePathAgain is a pointer -- it should instead be a well-dimensioned array, initialized with a lstrcpy or the like, so there's space for what you want to cat to it.

Alex Martelli
+1  A: 

To use lstrcat, drivePathAgain must be big enough to hold both strings; as you've got it, it's only big enough to hold "E:\".

Joe
A: 

I actually added this way before the code I posted:

char drivePathAgain[255]; 
lstrcpy((LPWSTR)drivePathAgain, (LPWSTR)drivePathTemp); 

with drivePathTemp = "E:\"; I've spent a while trying to debug this code to no avail. The declaration in my original post isn't what I used to declare, but rather to give people an idea of what the variable is like.

ffrstar777
Oh, and another thing, I wanted to say that drivePathtemp also has a string of "E:\\". but I copied it into drivePathAgain.
ffrstar777
If you're creating a buffer that can hold the largest possible path, you should use the MAX_PATH macro rather than a numeric constant. MAX_PATH is actually defined to be 260 characters: http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath
Josh Townzen
A: 

Based on your casting to LPWSTR, I would assume your project is setup in Unicode mode. That means functions like lstrcpy and MoveFile are accepting pointers to strings of wchar_t not char. If you don't know what this means, you need to research the difference between Ascii and Unicode.

I would suspect that may be the source of your problem. And even if it isn't, casting from char* to wchar_t* (also known as LPWSTR) will likely cause problems for you eventually. Casting pointers is not the same as converting from one of those string types to the other.

TheUndeadFish