Thread: how to convert char* to Cstring?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how to convert char* to Cstring?

    i have a function which the input is Cstring but i have a char array which i want to put in the fuction.

    how can i do this?

    by the way, what is CString?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    A char array is a C-string.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    but i cant put it in a function

    but how can i copy the char array to the Cstring?

    the Cstring is define in a class , when i use strcpy, error said that
    cant convert type "Class Cstring" to char*

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Are you using a c-string (char * or char[]) or a user defined class String??? If it is the latter you can´t assign two different classes without a conversion.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    A char string can be copied to a CString by doing:
    Code:
    char strString[] = "this is a char string";
    CString cstring;
    
    cstring = CString( strString );
    And pass a char string to a function which the input is CString.
    Code:
    foo( CString( strString ) );
    Last edited by Scarlet7; 04-27-2003 at 03:03 AM.

  6. #6
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    u can also do something like this..

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
          char bob[10] = "bob";
          char* doh = "yomama!";
          unsigned int i =0;
          while(i < (strlen(doh)))	bob[i++] = doh[i++];
          cout <<bob<<endl<<doh<<endl;
          return 0;
    }
    Or u could always define both either as char[] or char* and then use strcpy which is imho the easiest way to do it..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  7. #7
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Originally posted by Luigi
    u can also do something like this..

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
          char bob[10] = "bob";
          char* doh = "yomama!";
          unsigned int i =0;
          while(i < (strlen(doh)))	bob[i++] = doh[i++];
          cout <<bob<<endl<<doh<<endl;
          return 0;
    }
    Or u could always define both either as char[] or char* and then use strcpy which is imho the easiest way to do it..
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    This is you posting without understanding.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    >while(i < (strlen(doh)))bob[i++] = doh[i++];

    How many times is i being incrementing per loop! i++ twice

    Isn't this thread talking about how to copy the char array to a CString!
    Last edited by Scarlet7; 04-26-2003 at 03:38 PM.

  9. #9
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    sorry for trying to help
    i++ twice thing.. ur right my bad i made this in 5 sec..

    I guess from now on ill just ask question and wont bother trying to help others if i get answered like this..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  10. #10
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Hi Luigi, don't worry about it. I once posted void main() on this forum which is a big sin around here, much worse than the i++ thing.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    How about:

    char* foo="string";
    Cstring mystring(foo);

    It's a more effecient form of what scarlet said.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    3

    Thumbs down this is a very simple method

    this is a very simple method
    Code:
    public: void ToCString(CString* outputString)
    	{
                        if(outputString == NULL)outputString = new CString();
    	    outputString->Empty();
    	    for(int i=0;i<cbData;i++)
    	     {
    	           outputString->AppendChar(pbData[i]);
    	     }
    	 }

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you using a user-defined CString or MFC's CString?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Jasonymk View Post
    i have a function which the input is Cstring but i have a char array which i want to put in the fuction.

    how can i do this?

    by the way, what is CString?
    If it's the one I'm thinking of, it's Microsoft's string class. Is this a Windows program? Anyway, you need to create a CString from your character array and pass it to the function.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Duyetnv View Post
    this is a very simple method
    Code:
    public: void ToCString(CString* outputString)
    	{
                        if(outputString == NULL)outputString = new CString();
    	    outputString->Empty();
    	    for(int i=0;i<cbData;i++)
    	     {
    	           outputString->AppendChar(pbData[i]);
    	     }
    	 }
    Why not...

    CString temp(char_str);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert from int to cstring
    By mr_empty in forum C++ Programming
    Replies: 35
    Last Post: 12-07-2007, 01:59 AM
  2. Writing CObjects with Serialization to CArchive
    By ruben_gerad in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2006, 08:25 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 10:23 AM