CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Posts
    8

    CString & char[]

    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


  2. #2
    Join Date
    Apr 1999
    Posts
    383

    Re: CString & char[]

    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


  3. #3
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: CString & char[]

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


    --
    Jason Teagle
    [email protected]

  4. #4
    Join Date
    Apr 1999
    Posts
    8

    Re: CString & char[]

    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.


  5. #5
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: CString & char[]

    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?


    --
    Jason Teagle
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured