Click to See Complete Forum and Search --> : Basicstring::replace call


Kohinoor24
April 8th, 2003, 01:55 AM
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..

Manish Malik
April 8th, 2003, 05:52 AM
Can you put up the relevant portion of code, possibly along with the problematic input/output pair?

Kohinoor24
April 8th, 2003, 06:21 AM
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.

Paul McKenzie
April 8th, 2003, 10:58 AM
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:

#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

Kohinoor24
April 9th, 2003, 01:44 AM
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::out|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;

}

Paul McKenzie
April 9th, 2003, 04:36 AM
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.

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

Kohinoor24
April 9th, 2003, 04:40 AM
Okay,Thanks for the suggestion.I will use the standard String. For me its not returning 11.