ffrstar777
ffrstar777

Reputation: 75

How to concatenate using lstrcat in Visual C++?

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

Upvotes: 1

Views: 2337

Answers (4)

TheUndeadFish
TheUndeadFish

Reputation: 8171

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.

Upvotes: 1

ffrstar777
ffrstar777

Reputation: 75

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.

Upvotes: 0

Joe
Joe

Reputation: 42627

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:\".

Upvotes: 2

Alex Martelli
Alex Martelli

Reputation: 881705

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.

Upvotes: 3

Related Questions