From: sb on
Hello!

Would you help me convert string from char[50] to MFC class CString in
VC.NET, VC2005.

Sample code is:

char a[50];
fgets( a, 50, FILE);
CString b = new CString;
b = (CString)a;

I've tried several ways but useless... For now I solved this problem
with conversion by single character, i.e. a loop like this:

while( i<50 ) {
b += a[i];
}

but I hope there is a better way!

Thanks!

From: Heinz Ozwirk on
"sb" <leto82(a)gmail.com> schrieb im Newsbeitrag
news:1160907714.431440.149510(a)b28g2000cwb.googlegroups.com...
> Hello!
>
> Would you help me convert string from char[50] to MFC class CString in
> VC.NET, VC2005.
>
> Sample code is:
>
> char a[50];
> fgets( a, 50, FILE);
> CString b = new CString;

This shouldn't even compile. You cannot assign a pointer to a CString to a
CString (as you cannot assigne a pointer to a double to a double. When
writing C++ (or even C) code you should forget such Javaisms (or
C-sharpishnesses).

> b = (CString)a;

Such casts are almost always bad. When you need a cast and don't know why,
you are probably doing something wrong.

> I've tried several ways but useless... For now I solved this problem
> with conversion by single character, i.e. a loop like this:
>
> while( i<50 ) {
> b += a[i];
> }

A simple assignment or constructor should do the job. Try

CString b(a);

or

CString b;
b = a;

If that doesn't compile, you are probably compiling your code for Unicode
(default for VC2005). Go to the project settings and change the character
set from Unicode to "Multi-Byte Character Set".

And don't forget - CString and many other classes coming with VC are not
part of standard C++. Better post such questions to a Microsoft group like
microsoft.public.vc.language.

Regards
Heinz


From: sb on

"Heinz Ozwirk пиÑ?ал(а):
"
> "sb" <leto82(a)gmail.com> schrieb im Newsbeitrag
> news:1160907714.431440.149510(a)b28g2000cwb.googlegroups.com...
> > Hello!
> >
> > Would you help me convert string from char[50] to MFC class CString in
> > VC.NET, VC2005.
> >
> > Sample code is:
> >
> > char a[50];
> > fgets( a, 50, FILE);
> > CString b = new CString;
>
> This shouldn't even compile. You cannot assign a pointer to a CString to a
> CString (as you cannot assigne a pointer to a double to a double. When
> writing C++ (or even C) code you should forget such Javaisms (or
> C-sharpishnesses).
>
> > b = (CString)a;
>
> Such casts are almost always bad. When you need a cast and don't know why,
> you are probably doing something wrong.
>
> > I've tried several ways but useless... For now I solved this problem
> > with conversion by single character, i.e. a loop like this:
> >
> > while( i<50 ) {
> > b += a[i];
> > }
>
> A simple assignment or constructor should do the job. Try
>
> CString b(a);
>
> or
>
> CString b;
> b = a;
>
> If that doesn't compile, you are probably compiling your code for Unicode
> (default for VC2005). Go to the project settings and change the character
> set from Unicode to "Multi-Byte Character Set".
>
> And don't forget - CString and many other classes coming with VC are not
> part of standard C++. Better post such questions to a Microsoft group like
> microsoft.public.vc.language.

I tried both that ways but both led to errors too... But maybe that's
because of Unicode character set. So I'll try again with Multibyte,
thanks!

From: Ulrich Eckhardt on
sb wrote:
> Would you help me convert string from char[50] to MFC class CString in
> VC.NET, VC2005.
>
> Sample code is:
>
> char a[50];
> fgets( a, 50, FILE);
> CString b = new CString;
> b = (CString)a;
>
> I've tried several ways but useless...

....as above code shows. The primary problem is that CString is based on
TCHAR, which sometimes evaluates to char and sometimes to wchar_t. So,
either you use TCHAR-based functions and types all the way through or you
use char with CStringA (or wchar_t with CStringW) or you convert.
Typically, the way to go is way 2, but all this is outside the scope of a
C++ newsgroup; it rather belongs to a win32 related newsgroup.



> For now I solved this problem
> with conversion by single character, i.e. a loop like this:
>
> while( i<50 ) {
> b += a[i];
> }
>
> but I hope there is a better way!

Even the TCHAR-based CString has a ctor that takes a char-based string
which is then converted accordingly. But again, this doesn't strictly
belong here.

Uli

--
FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html