Assigning char * to CString Object :: C++

This is a discussion on Assigning char * to CString Object :: C++ within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I have a char * to a data buffer. What is the best way to assign data from char ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Assigning char * to CString Object :: C++

    Hi.

    I have a char * to a data buffer. What is the best way to assign data from char * into a CString object? For example:

    -----
    // char *myChar = new char[10];
    // memcpy(myChar, "abcdefghij", 10);
    // CString myString;
    -----

    What is the best say to insert data from myChar into myString?

    Thanks,
    Kuphryn

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    
    {
    	char *myChar;
    	string myString;
    
    	myChar = new char[11];
    	strcpy(myChar,"abcdefghij");
    	myString = myChar;
    	cout << myString <<endl;
    	return 0;
    }
    That what you want?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    That is similar to my final code.

    -----
    // CString myString = CString("abcdefghij");
    -----

    Kuphryn

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    There also is a couple of methods that work well. One that comes to mind is .Add.

    CString csTest;
    csTest.Add(string);

    You also can do

    CString csTest = "Blah Blah"; I don't think you need to cast that.

    Although I haven't used CString for awhile and I don't have a compiler here so I might be off on some of this stuff.
    Last edited by Barjor; 05-21-2002 at 10:00 AM.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Kuphryn

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    If I'm not mistaken, CString allows explicit casting to LPCSTR, so you could simply make CStringVar = charVar;

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Yes. You can cast a CString object to character array using LPCSTR.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 06:53 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 09:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21