CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Basicstring::replace call

    I have problems with the basicstring::replace function of the following type under Linux.
    s.replace(2, 2, s); & Then If I call an append right after this call,It is not appending to the end of string,but appends at the position after the replace position.

    Iam using Caldera Linux 3.0.1
    Thanks..

  2. #2
    Join Date
    Feb 2001
    Location
    teh INTARWEB
    Posts
    542
    Can you put up the relevant portion of code, possibly along with the problematic input/output pair?

  3. #3
    Join Date
    Oct 2001
    Posts
    745
    I have a base class C_SF.

    T_Byte C_SF::s_sfIntroducer[] ={'\x5A','\x00','\x00','\xFF','\xFF','\xFF','\x00','\x00','\x00'};
    T_ByteArray C_SF::s_unitPerUnitBase= T_ByteArray('\x38','\x40');
    int C_SF::s_sfIdentifierLength = 3;
    int C_SF::s_sfIdentifierPosition = 3;

    void C_SF::Init()
    {
    m_byteRepresentation.assign(s_sfIntroducer,sizeof(s_sfIntroducer));
    }


    C_OBP::C_OBP()
    :C_SF()
    {
    Init();
    }

    c_OBP is a derived class from C_SF.
    **********************************
    void C_OBP::Init()
    {

    // Problem is calling the following replace
    m_byteRepresentation.replace(s_sfIdentifierPosition,s_sfIdentifierLength,s_sfIdentifier);

    m_byteRepresentation.append(Obp,sizeof(Obp));
    }




    In some places this replace is working fine.Here not.Why is it that it is working somewhere & somewhere not.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Kohinoor24
    In some places this replace is working fine.Here not.Why is it that it is working somewhere & somewhere not.
    What is Obp? Are you attempting to append an object onto a string? Unless there is an overriden char*() or std::string operator, that append() is very strange.

    If you would post a small, compilable example with the data that doesn't work, maybe someone can help. We don't have your program or know what any of the values are for the call to replace or append(), whether you've trashed memory somewhere else in your program, etc.

    The replace and append are standard string functions -- all we need from you is to give us a very small program (an int main()) with the hard-coded data that duplicates your problem. Since the claim is that there is a problem with replace and append, why not just test these functions stand-alone? There is no need for T_ByteArray's (whatever that is) or anything else:
    Code:
    #include <string>
    
    int main()
    {
        std::string sTest;
        //...
        sTest.replace( hard_coded_number1, hard_coded_number2,
                               hard_coded_chars);
        sTest.append(  hard_coded_data, hard_coded_number)
    }
    Something similar to the code above (maybe add a loop to do this 1,000 times or so). Then everyone, including us that may be running VC++, can test your code and verify whether or not it is a bug with the compiler.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2001
    Posts
    745
    First of all Thanks for your time & help.
    Iam posting a small code here.
    This is not working under windows as well

    #include<string>
    #include<iostream>
    #include <fstream>
    using namespace std;

    typedef std::basic_string<unsigned char> T_ByteArray;

    ofstream out("File",ios:ut|ios::binary);


    int main()
    {

    T_ByteArray m_byteRepresentation;
    int s_sfIdentifierLength = 3;
    int s_sfIdentifierPosition =3;

    unsigned char s_escapeSequence[] = {'\x2B','\xD3'};
    unsigned char s_sfIdentifier[] ={'\xD3','\xAC','\x6B'} ;
    unsigned char s_sfIntroducer[] ={'\x5A','\x00','\x00','\xFF','\xFF','\xFF','\x00','\x00','\x00'};
    m_byteRepresentation.assign(s_sfIntroducer,sizeof(s_sfIntroducer));
    int x =m_byteRepresentation.size();
    m_byteRepresentation.replace(s_sfIdentifierPosition,s_sfIdentifierLength,s_sfIdentifier);
    m_byteRepresentation.append(s_escapeSequence,sizeof(s_escapeSequence));
    out.write(reinterpret_cast<const char*>(m_byteRepresentation.c_str()),m_byteRepresentation.size());
    out.close();


    return 0;

    }

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Kohinoor24
    First of all Thanks for your time & help.
    Iam posting a small code here.
    This is not working under windows as well

    #include<string>
    #include<iostream>
    #include <fstream>
    using namespace std;

    typedef std::basic_string<unsigned char> T_ByteArray;
    Why not just use std::string? It handles binary data correctly.
    Code:
    typedef std::string T_ByteArray;
    
    int main()
    {	
       T_ByteArray m_byteRepresentation;
        int s_sfIdentifierLength = 3;
        int s_sfIdentifierPosition =3;
    
        char s_escapeSequence[] = {'\x2B','\xD3'};
        char s_sfIdentifier[] ={'\xD3','\xAC','\x6B'} ;
        char s_sfIntroducer[] ={'\x5A','\x00','\x00','\xFF','\xFF','\xFF','\x00','\x00','\x00'};
    
        m_byteRepresentation.assign(s_sfIntroducer,sizeof(s_sfIntroducer));
        int x =m_byteRepresentation.size();
        m_byteRepresentation.replace(s_sfIdentifierPosition,s_sfIdentifierLength,s_sfIdentifier);	
        m_byteRepresentation.append(s_escapeSequence,sizeof(s_escapeSequence));	
        cout << "length of string is " << m_byteRepresentation.length();
        return 0;
    }
    
    Output:
    
    length of string is 11
    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2001
    Posts
    745
    Okay,Thanks for the suggestion.I will use the standard String. For me its not returning 11.

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