I Phil Good
April 16th, 1999, 06:11 AM
Hello,
How can i convert a char array like toto[10] into Cstring object and how can i include a LF char into CString object.
Thank you
Dave Lorde
April 16th, 1999, 06:18 AM
char toto[10] = "123456789";
// 2 variations on the construction theme:
CString str1 = toto;
CString str2(toto);
// Add a line feed (newline) character
str1 += '\n';
// Add carriage return & line feed
str2 += "\r\n";
Dave
Jason Teagle
April 16th, 1999, 06:18 AM
char array to CString:
CString strString = toto ;
To add an LF character:
strString += '\n';
(Note that you could store this in your char array:
strcat(toto,"\n");
and then convert to CString, if you wanted).
I Phil Good
April 16th, 1999, 07:30 AM
is it working with unsigned char ? I've tried yesterday and it didn't work.
Can y use cstring in CFile.Read function ?
Thank for your help.
Jason Teagle
April 16th, 1999, 08:55 AM
CString simply stores a string of characters - it does not make any difference whether they are signed or unsigned. For unsigned characters you will have to do a small cast when you access the characters. I.e.,
---
unsigned char toto[] = "Hello!";
CString strTemp ;
toto[0] = (unsigned char)0xFE ;
strTemp = toto ; // Effectively stored as signed, but it has still
// got the value (ASCII) 0xFE, it just treats it
// as -2.
printf("%d", strTemp[0]); // Generates "-2".
printf("%u", strTemp[0]); // Generates "254".
// Anywhere else you want to use the character, you should cast to
// (unsigned char).
// Note that if you use that cast in printf functions, it produces
// an unusual number; printf messes up the number and ends up with
// the DWORD form of (-2), i.e. something large. I don't know why -
// but as you can see, using "%u" with the signed version works
// correctly. Strange but true.
// Note also that CString has a [] operator, which can be used like
// an array [].
---
Yes, a CString can be used in CFile::Read(), but as a buffer only. Like this:
---
CFile filInput ;
char *pcBuffer ;
CString strBuffer ;
pcBuffer = strBuffer.GetBuffer(MAX_SIZE);
filInput.Read(pcBuffer, MAX_SIZE);
strBuffer.ReleaseBuffer(MAX_SIZE);
---
However, if you use CStdioFile, then you can use CStdioFile::ReadString(), which accepts a reference to a CString.
Does this help?