How to Copy char* string to WCHAR[] string

M

Manjunath.M

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
 
I

Ian Collins

Manjunath.M said:
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.
 
M

Manjunath.M

Manjunath.M said:
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.
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.
 
I

Ian Collins

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
 
M

Manjunath.M

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.
for this try to include wchar.h.
actual WCHAR means internally defined as DWORD.


Thanks,
Manjunath.M
 
I

Ian Collins

Ian said:
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;
}
 
I

Ian Collins

Manjunath.M said:
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.
 
M

Manjunath.M

Please trim signatures!


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


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.
 
I

Ian Collins

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, ...);
 
S

santosh

Manjunath.M said:
Manjunath.M said:
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.
 
C

CBFalconer

Manjunath.M said:
Manjunath.M said:
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).
 
C

CBFalconer

Manjunath.M said:
.... 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.
 
S

santosh

Manjunath.M said:
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.
 
M

Manjunath.M

Sorry if I am rude.
However my intention is just to overcome the problem facing, with all
your help.
 
S

santosh

Manjunath.M said:
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?
 
O

Old Wolf

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.
 
S

santosh

Old said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top