473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to Copy char* string to WCHAR[] string

Hi,
I wrote a simple program.

WCHAR NameBuffer[512];
char * str1 = "c:\\Program Files\\test.txt" ;
swprintf(NameBuffer,L"%s",str1);

I tried to debug this program. value at "Namebuffer" is showing some
garbage values like square symbol.how to resolve this.

please can any one help me ...
Thanks,
Manjunath.M

Feb 22 '07 #1
18 19664
Manjunath.M wrote:
Hi,
I wrote a simple program.

WCHAR NameBuffer[512];
What is WCHAR?
char * str1 = "c:\\Program Files\\test.txt" ;
swprintf(NameBuffer,L"%s",str1);
The above looks like syntax error.
I tried to debug this program. value at "Namebuffer" is showing some
garbage values like square symbol.how to resolve this.
Please post the real program, the above certainly isn't it.

--
Ian Collins.
Feb 22 '07 #2
On Feb 22, 11:53 am, Ian Collins <ian-n...@hotmail.comwrote:
Manjunath.M wrote:
Hi,
I wrote a simple program.
WCHAR NameBuffer[512];

What is WCHAR?
char * str1 = "c:\\Program Files\\test.txt" ;
swprintf(NameBuffer,L"%s",str1);

The above looks like syntax error.
I tried to debug this program. value at "Namebuffer" is showing some
garbage values like square symbol.how to resolve this.

Please post the real program, the above certainly isn't it.

--
Ian Collins.
int main()
{
WCHAR NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt"
swprintf(NameBuffer,L"%s",str1);
printf("%s",NameBuffer);
return 0;
}
This is the program written in C.

Feb 22 '07 #3
Manjunath.M wrote:
>>
Please post the real program, the above certainly isn't it.
Please trim signatures.
int main()
{
WCHAR NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt"
swprintf(NameBuffer,L"%s",str1);
printf("%s",NameBuffer);
return 0;
}

This is the program written in C.
It doesn't look like it:

cc x.c
"x.c", line 12: undefined symbol: WCHAR
"x.c", line 12: syntax error before or at: NameBuffer
"x.c", line 14: syntax error before or at: swprintf
"x.c", line 15: warning: implicit function declaration: printf
"x.c", line 15: undefined symbol: NameBuffer

--
Ian Collins.
Feb 22 '07 #4
On Feb 22, 12:11 pm, Ian Collins <ian-n...@hotmail.comwrote:
Manjunath.M wrote:
>Please post the real program, the above certainly isn't it.

Please trim signatures.
int main()
{
WCHAR NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt"
swprintf(NameBuffer,L"%s",str1);
printf("%s",NameBuffer);
return 0;
}
This is the program written in C.

It doesn't look like it:

cc x.c
"x.c", line 12: undefined symbol: WCHAR
"x.c", line 12: syntax error before or at: NameBuffer
"x.c", line 14: syntax error before or at: swprintf
"x.c", line 15: warning: implicit function declaration: printf
"x.c", line 15: undefined symbol: NameBuffer

--
Ian Collins.
I wrote this program in VC++ Windows Operating System.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.
Thanks,
Manjunath.M

Feb 22 '07 #5
Ian Collins wrote:
Manjunath.M wrote:
>>>Please post the real program, the above certainly isn't it.


Please trim signatures.

>>int main()
{
WCHAR NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt"
swprintf(NameBuffer,L"%s",str1);
printf("%s",NameBuffer);
return 0;
}

This is the program written in C.

It doesn't look like it:

cc x.c
"x.c", line 12: undefined symbol: WCHAR
"x.c", line 12: syntax error before or at: NameBuffer
"x.c", line 14: syntax error before or at: swprintf
"x.c", line 15: warning: implicit function declaration: printf
"x.c", line 15: undefined symbol: NameBuffer
I think what you wanted was more like

#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt";
swprintf(NameBuffer,100,L"%s",str1);
printf("%s\n",NameBuffer);
return 0;
}


--
Ian Collins.
Feb 22 '07 #6
Manjunath.M wrote:
>>
>>>This is the program written in C.

It doesn't look like it:

cc x.c
"x.c", line 12: undefined symbol: WCHAR
"x.c", line 12: syntax error before or at: NameBuffer
"x.c", line 14: syntax error before or at: swprintf
"x.c", line 15: warning: implicit function declaration: printf
"x.c", line 15: undefined symbol: NameBuffer
Please trim signatures!
>
I wrote this program in VC++ Windows Operating System.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.
You must have had a compiler error for the missing semicolon and a
warning for the wrong second parameter type for swprintf.

--
Ian Collins.
Feb 22 '07 #7
On Feb 22, 12:26 pm, Ian Collins <ian-n...@hotmail.comwrote:
Manjunath.M wrote:
>>This is the program written in C.
>It doesn't look like it:
>cc x.c
"x.c", line 12: undefined symbol: WCHAR
"x.c", line 12: syntax error before or at: NameBuffer
"x.c", line 14: syntax error before or at: swprintf
"x.c", line 15: warning: implicit function declaration: printf
"x.c", line 15: undefined symbol: NameBuffer

Please trim signatures!
I wrote this program in VC++ Windows Operating System.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.

You must have had a compiler error for the missing semicolon and a
warning for the wrong second parameter type for swprintf.

--
Ian Collins.

I forgot to place the semicolon here . it is not showing warning.

when i pt the cursur on the WCHAR it showing a tool tip as "typedef
DWORD WCHAR"

I think it is not a wchar_t.
Feb 22 '07 #8
Manjunath.M wrote:
On Feb 22, 12:26 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>
You must have had a compiler error for the missing semicolon and a
warning for the wrong second parameter type for swprintf.
One last try, please trim signatures, the bit after the "-- ". Your
news reader should do this by default.
>
I forgot to place the semicolon here . it is not showing warning.
Time for a new compiler...
>
when i pt the cursur on the WCHAR it showing a tool tip as "typedef
DWORD WCHAR"

I think it is not a wchar_t.
No, that's the windows version, the standard version is:

int swprintf(wchar_t *restrict s,
size_t n,
const wchar_t *restrict format, ...);

--
Ian Collins.
Feb 22 '07 #9
Manjunath.M wrote:
On Feb 22, 12:11 pm, Ian Collins <ian-n...@hotmail.comwrote:
Manjunath.M wrote:
>>Please post the real program, the above certainly isn't it.
Please trim signatures.
int main()
{
WCHAR NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt"
swprintf(NameBuffer,L"%s",str1);
printf("%s",NameBuffer);
return 0;
}
This is the program written in C.
It doesn't look like it:

cc x.c
"x.c", line 12: undefined symbol: WCHAR
"x.c", line 12: syntax error before or at: NameBuffer
"x.c", line 14: syntax error before or at: swprintf
"x.c", line 15: warning: implicit function declaration: printf
"x.c", line 15: undefined symbol: NameBuffer

I wrote this program in VC++ Windows Operating System.
Visual C++ isn't an operating system. It's a compiler system for C and
C++.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.
If you're using Windows extensions like WCHAR, a Windows targetted
group like comp.os.ms-windows.programmer.win32 would be more
appropriate.

For doing your program in standard C, substitute WCHAR with wchar_t
and include the wchar.h header.

Also don't forget to terminate your str1 initialisation with a
semicolon.

Feb 22 '07 #10
"Manjunath.M" wrote:
On Feb 22, 11:53 am, Ian Collins <ian-n...@hotmail.comwrote:
>Manjunath.M wrote:
>>>
I wrote a simple program.
>>WCHAR NameBuffer[512];

What is WCHAR?
>>char * str1 = "c:\\Program Files\\test.txt" ;
swprintf(NameBuffer,L"%s",str1);

The above looks like syntax error.
>>I tried to debug this program. value at "Namebuffer" is showing some
garbage values like square symbol.how to resolve this.

Please post the real program, the above certainly isn't it.
int main()
{
WCHAR NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt"
swprintf(NameBuffer,L"%s",str1);
printf("%s",NameBuffer);
return 0;
}

This is the program written in C.
Can't be the actual program. Lacks critical includes, a #define of
WCHAR, etc.

Don't you ever read the documentation for functions? N869:

7.24.2.3 The swprintf function

Synopsis

[#1]
#include <wchar.h>
int swprintf(wchar_t * restrict s,
size_t n,
const wchar_t * restrict format, ...);
Description

[#2] The swprintf function is equivalent to fwprintf, except
that the argument s specifies an array of wide characters
into which the generated output is to be written, rather
than written to a stream. No more than n wide characters
are written, including a terminating null wide character,
which is always added (unless n is zero).

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 22 '07 #11
"Manjunath.M" wrote:
>
.... snip ...
>
I wrote this program in VC++ Windows Operating System.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.
If you want help, why do you conceal the actual program? Obviously
you don't care to present the actual problem, and thus need no
help.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 22 '07 #12
On Feb 22, 2:35 pm, CBFalconer <cbfalco...@yahoo.comwrote:
"Manjunath.M" wrote:

... snip ...
I wrote this program in VC++ Windows Operating System.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.

If you want help, why do you conceal the actual program? Obviously
you don't care to present the actual problem, and thus need no
help.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
I am using this function not the one which u mentioned .
check this link

for this one no need of wchar.h

http://msdn2.microsoft.com/fr-fr/library/ms860374.aspx

Feb 22 '07 #13
Manjunath.M wrote:
On Feb 22, 2:35 pm, CBFalconer <cbfalco...@yahoo.comwrote:
"Manjunath.M" wrote:

... snip ...
I wrote this program in VC++ Windows Operating System.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.
If you want help, why do you conceal the actual program? Obviously
you don't care to present the actual problem, and thus need no
help.

I am using this function not the one which u mentioned .
check this link

for this one no need of wchar.h

http://msdn2.microsoft.com/fr-fr/library/ms860374.aspx
How many times should you be requested not to quote signatures? The
text after '-- ' should be snipped off, unless you're commenting upon
it.

Also please use proper words instead of abominations like 'u', '4' and
'plz'.

Finally, coming to your post, this group discusses ISO standardised C,
wherein the proper way to do what you seem to want involves wchar.h
and the wchar_t type. If you still want to use Microsoft's non-
standard headers and types please post further questions on the
subject to a more appropriate group like comp.os.ms-
windows.programmer.win32 or one of the microsoft.public.* groups.

Feb 22 '07 #14
Sorry if I am rude.
However my intention is just to overcome the problem facing, with all
your help.
Feb 22 '07 #15
"Manjunath.M" wrote:
CBFalconer <cbfalco...@yahoo.comwrote:
>"Manjunath.M" wrote:

... snip ...
>>I wrote this program in VC++ Windows Operating System.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.

If you want help, why do you conceal the actual program? Obviously
you don't care to present the actual problem, and thus need no
help.

I am using this function not the one which u mentioned .
check this link
u never mentioned any function, in fact he does not post here.
>
for this one no need of wchar.h

http://msdn2.microsoft.com/fr-fr/library/ms860374.aspx
Any code from Microsoft is probably faulty. You still refuse to
post your actual code. We should ignore you from now on.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Feb 22 '07 #16
Manjunath.M wrote:
Sorry if I am rude.
However my intention is just to overcome the problem facing, with all
your help.
Well, Ian Collins has given a standardised way to do what you want.
Once again, for your convinience, I'll repost it below:

#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt";
swprintf(NameBuffer,100,L"%s",str1);
printf("%s\n",NameBuffer);
return 0;

}

Have you compiled the code? Does it meet your needs?

Feb 22 '07 #17
On Feb 22, 8:22 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ian Collins wrote:
#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt";
swprintf(NameBuffer,100,L"%s",str1);
printf("%s\n",NameBuffer);
return 0;
}
The printf() call causes undefined behaviour because you passed
a pointer for %s that isn't compatible with (char *). In
practice you are likely to get blank output or perhaps a single
letter 'c', because the wide character equivalent of narrow 'c'
is likely to be 'c' and a null narrow character juxtaposed.
Feb 22 '07 #18
Old Wolf wrote:
On Feb 22, 8:22 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ian Collins wrote:
#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt";
swprintf(NameBuffer,100,L"%s",str1);
printf("%s\n",NameBuffer);
return 0;
}

The printf() call causes undefined behaviour because you passed
a pointer for %s that isn't compatible with (char *). In
practice you are likely to get blank output or perhaps a single
letter 'c', because the wide character equivalent of narrow 'c'
is likely to be 'c' and a null narrow character juxtaposed.
The OP can use wprintf, declared in wchar.h instead.

Feb 22 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
3
by: Lars Nielsen | last post by:
Hey there I have a win32 application written i c++. I have a std::vector of std::string's i will fill with filenames. typedef vector<std::string> strvector; strvector vFiles; ...
3
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; for(int i=0;...
1
by: Kieran Benton | last post by:
Hi, Sorry to post this, I feel like a right fool but Im under serious time pressure! Afraid I'm a newbie to managed C++ (Ive had to resort to it as Im wrapping some COM objects for C# use). Any...
5
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
4
by: sweety | last post by:
Dear all, Kindy help to convert the char* ( LPCSTR) to WCHAR*(LPCWSTR). Would be great if you tell if any function does this job in VC++. Quick response will be greatfull...as its blocked... ...
6
by: MattWilson.6185 | last post by:
Hello! I am trying to convert a char * to a LPWSTR, and I am going absolutly mad! I can't find anything besides typle L"string" Unfortunetaly I can't use that... basicaly the setup is ...
10
by: Erik Knudsen | last post by:
Hi! In converting applications from ansi to unicode, it is a problem that the std::wcout accepts a const char * without complaining compile time. This compiles and runs:...
2
by: Akino877 | last post by:
Hello, I have a small program which uses FindFirstFile() to get a file name. Once I get the file name, I would like to use sscanf to extract certain part of the file name. My program is as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.