These things are getting on my nerves. I am trying to do a test program in Win32 API which has "Browse for folder" and uses some string formatting. But, I am unable to.

Please help in this general problem:
If I use strcpy() in Visual Studio 2008 to do some string related things, the problem is only the first character of the dest_str is replaced from the source_str. For Ex:

char S1[] = "C:\\Program Files" ;\\Also tried wchar_t
char S2[]= "Zebra";
strpy(S1,S2); \\will have to type-cast paramaters to(char *) if wchar_t is used

Now, if I display S1, it outputs : Z:\Program files
What the heck^ ?

I had to manually do a while() loop to simulate strcpy function. But, this while() cannot be used to do anything sprintf() or similar does. If I use sprintf(), I'll have to put the first parameter as char, the second parameter cannot be made into any WSTR as sprintf doesn't understand that. Now, if I display in a messagebox the string from sprintf, typecasted in LPWSTR, it displays boxes, instead of letters.
As, an alternative if I use _itoa() or strcat, it results in the same problems as I have mentioned above(with typecasting problems too).
Any "C" style string functions doesn't work in the program.

MAN! I'm stuck in a very big problem! Now, I googled and got some info on str.Format(). But, I cannot use it as compiler reports an error that left of .Format must be struct/namespace/blah blah. I cannot either use Cstring as the "Cstring" idientifier is not found!!! I think I have used all the header files I knew of and none worked!

Now, can anyone help me here? What do I use for string formatting in a Win32 Shell Program(as may be strcpy or strncpy is deprecated)? Or is the compiler gone mad? Is there any alternative other than the things mentioned above?

Thanks in advance!

you can not just typecase from char* to wchar_t*. You have to call one of the transformation functions.

This works ok for me when the program is not compiled for UNICODE

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char S1[] = "C:\\Program Files" ;
char S2[]= "Zebra";
strcpy(S1,S2);
cout << S1 << "\n";
}

And the UNICODE version

#include <iostream>
#include <tchar.h>
using namespace std;
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
int main()
{
    TCHAR S1[] = _TEXT("C:\\Program Files") ;
    TCHAR S2[]= _TEXT("Zebra");
    _tcscpy(S1,S2);
    COUT << S1 << _TEXT("\n");
}

With the above code you can compile the program with our without UNICODE and the compiler will make appropriate conversions for you.

If you are not really interested in UNICODE then just turn it off and code normally as in my first code snippet. To turn it off select menu item Project --> Properties --> Configuration Properties --> General. Then on the right side of the screen change the "Character Set" list box to "Not Set".

commented: Thanks for the quick help +1

you can not just typecase from char* to wchar_t*. You have to call one of the transformation functions.

This works ok for me when the program is not compiled for UNICODE

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char S1[] = "C:\\Program Files" ;
char S2[]= "Zebra";
strcpy(S1,S2);
cout << S1 << "\n";
}

And the UNICODE version

#include <iostream>
#include <tchar.h>
using namespace std;
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
int main()
{
    TCHAR S1[] = _TEXT("C:\\Program Files") ;
    TCHAR S2[]= _TEXT("Zebra");
    _tcscpy(S1,S2);
    COUT << S1 << _TEXT("\n");
}

With the above code you can compile the program with our without UNICODE and the compiler will make appropriate conversions for you.

If you are not really interested in UNICODE then just turn it off and code normally as in my first code snippet. To turn it off select menu item Project --> Properties --> Configuration Properties --> General. Then on the right side of the screen change the "Character Set" list box to "Not Set".

Well, the second solution you gave (_tcscpy()) worked great. Thanks for giving the UNICODE version of it. I assume that _tcscat and such will be the equivalent of char in wchar_t.

Can you also please tell me the UNICODE equivalent of sprintf()?

And, about UNICODE, is there any reason, I should keep it on or to remove it for safe Win32 programs?

Thanks a lot Ancient Dragon. Respect to you.

you can find all the unicode version of standard functions by googling for the function then selecting the MS-Windows version in the google links.

For programs I'm going to use just for myself I always turn UNICODE off so that I don't have to bother with the macros. You should do the same for code you are writing for university classes unless your professor tells you otherwise he/she wants UNICODE ready code.

I found swprintf() -- Thanks.

Oh! so, UNICODE is going to make no difference! So, I'll switch off UNICODE for my main project. Thanks a lot Ancient Dragon. You saved my day. [[B]Bad that I cannot give more reputation to you today! :([/B]]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.