|
-
June 10th, 2002, 10:03 AM
#1
Does anyone have a Solution
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
-
June 10th, 2002, 02:53 PM
#2
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.
-
June 10th, 2002, 03:40 PM
#3
Put your data into a std::string. Then call:
Code:
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|