Thread: string manipulation

  1. #1
    Unregistered
    Guest

    string manipulation

    Hi,

    how do you copy the value from a char * variable to a string without losing any data?

    i have tried directly input, for example

    char * someChar;
    strcpy(someChar,"somestring");

    string someStr = someChar;


    but i got some last chars missing while the char string is long.

    thanks for your help.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Give a size to the pointer!

    you can write
    string theString = "This is the long text...";

    this will allocate memory for it automatically.
    You can also do this:

    char theCharArray[] = "This is the long text...";

    this will allocate memory compile time, or just

    const char* theChars = "This is the long text...";

    but the last will make the pointer "theChars" point to a memory location inside your code, so you should not change it (e.g. you cannot write strcpy(theChars, .....);

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Can you give a running program which demonstrates your problem ? It's very hard to guess without a running program and example output.

    You need to allocate memory for your char* before copying to it.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Unregistered
    Guest
    it's actually a function that extract string from the char * into several part according to the standard specified.

    below is part of the function:

    long CWrapper::doExtract(char* response, Resp_doTransaction* resp)
    {

    //------------------------------------
    long retCode = 1;
    long retType = 0;
    int i;
    string strResp = response;

    .
    .
    .
    }

    the strlen for response is 900 chars, but after assigning to strResp, only 890 chars left and last few chars have been changed to different chars.

    Thanks for ur help.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Try this

    #include <assert.h>

    long CWrapper::doExtract(char* response, Resp_doTransaction* resp)
    {

    //------------------------------------
    long retCode = 1;
    long retType = 0;
    int i;

    int nLen = strlen(response);
    string strResp = response;
    assert(nLen == strResp.length());

    .
    .
    .
    }

    See if the assertion fails exactly here, if not the problem is elsewhere

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    It shouldn't be a general problem, the following program runs fine.
    Make sure the pointer you receive contains what you want it to
    hold. Using assertions like above is a good method for verifying that.


    PHP Code:
    #include <string.h>
    #include <string>
    #include <iostream>

    using namespace std;

    string test( const chartocopy )
    {
        
    string s tocopy;

        if( 
    strcmps.c_str(), tocopy ) != )
        {
            
    cout << s;
        }

        return 
    s;
    }


    int main()
    {
        
    char szBuffer[1000];

        
    memsetszBuffer0999 );
        
    memsetszBuffer'A'900 );

        
    charpc szBuffer;

        
    string str testpc );

        if( 
    strcmpstr.c_str(), pc ) != )
        {
            
    cout << str;
        }

        
    cout << endl << "the end. no errors.";

        return 
    0;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Unregistered
    Guest
    the program is running finely, but the string after assignment is still not the same as the original one.

    both return same length, and i did cout for both response (before assignment) and strResp (after assignment), response return correct string while strResp return incomplete string.

  8. #8
    Unregistered
    Guest
    it's really strange, i used strcmp as u suggested, it return equal, but while i cout or write to log file, it's just different from it's original string.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    There is a possibility in your implementation of class "string" to present a constructor from void*, or char*, which is different from that from const char*,
    so do this:


    string yourString = (const char*)response;

    This will ensure propper construction

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I tested nv's code and it worked.

  11. #11
    Unregistered
    Guest
    nv's code worked on mine too, but the result still appeared differently, below is part of the string:

    original:
    ...cT48L01lc3NhZ2U+PC9WUEFTPg==

    after assignment:
    ...cT48L01lc3NhZðÿÿÿ

    i have done (const char*) as well, but the result is still the same.

    i really have no idea why is this, and i really appreciate your kindly help whether it worked or not.

  12. #12
    Unregistered
    Guest
    i tried to change the structure of my code, and now when i do string assinment, it says fragmentation fault. why's that?

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Post your whole code. Lets see what is really going on. Also what compiler and operating system are you using?

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    You have serious problems with the memory. Maybe you have already accessed memory which you shouldn't: null pointers or out of array boundaries. That may happen before the code that crashes. By the way what compiler do you use?

  15. #15
    Unregistered
    Guest
    the code is simply too large to post here, but the portion i posted here is the main part that do the thing.

    i used g++ on redhat 6.2, and i did malloc/free all the char * i've used. to extract the msg from the char * and assigned to another variable in order to pass to another function, i used

    char* newVar = strndup(reference + advCount, sizeHere);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM