Click to See Complete Forum and Search --> : Does anyone have a Solution


Kohinoor24
June 10th, 2002, 10:03 AM
I need a solution for this.
I have a buffer char *m_pBuffer;
I have a function like this

unsigned int WriteData(char *pData,int length);

which takes a chracter array & the no: of bytes to be put into the buffer as parameters.Iam writing into the buffer using the memcpy() function.

The real problem starts now.I have to read data from a list now & write into the buffer using the Writeadata() function.ie: reading the data into the character array declared as parameterin the write function() & calculate the no: of bytes read to put into the 2nd parameter.
If someone can show me how this is done.The rest of the part I can do(Writing into the buffer)

the list is somewhat like this:
for(NgDocData<B>::const_iterator it = ngDocData.m_ngSubDocDataList.begin();
it != ngDocData.m_ngSubDocDataList.end(); it++)

How can I do this?
Please show me with a code snippet
Thanks in advance

JMS
June 10th, 2002, 02:53 PM
I'll take a crack at this but I'm not sure what your asking.


>> & calculate the no: of bytes read to put into the 2nd
>> parameter.

1) If you are working with strings then you could just use strlen.

int iLength = strlen ( szBuffer );

2) If you're not working with strings then you could still use strlen.
In this case set the buffer you're storing the string segment into to all nulls..

char szBuffer[100];

memset( (void *)szBuffer, "\0", sizeof(szBuffer));

then as you add each character to the beginning of the array your resulting char array will always be Null terminated as long as it doesn't overflow the szBuffer array.

3) The last technique you could use is just calculate the length of the array dirrectly from the beginning and end pointers.

// setting up my char pointers to the front and end of the string
char *pszBuffer = "Yippee Yippee Zipy";
char *pszEndBuffer = pszBuffer+17;


int iLength = (int)(pszEndBuffer - pszBuffer);


Of all three of these solutions I'd probable go with #1, then #2 and only then try #3. #3 is a reasonable solution but creating a string out of your character array as called for in solution #2 will probable serve you best and be least confusing to whoever has to support your code in the future.

But that's a religous question and I'm a secular dude.

Graham
June 10th, 2002, 03:40 PM
Put your data into a std::string. Then call:

WriteData(s.c_str(), s.length()); // s is a std::string

Of course, you'll have to change WriteData to take a const char*, but it should probably have been that, anyway. Or modify it to take a const reference to a string, then you don't need the length argument.