-delete this thread-
-delete this thread-
Last edited by bobbelPoP; 08-12-2008 at 12:07 PM.
Of course.Though thing is, is this possible with char/char*?
By the way, your implementation of chunk_split is incorrect.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You possible return a boolean (false) when the function should output an std::string.
concatenation of two strings is simply an "operator+" , you don't have to loop (you will have to loop for char* , that's why you should always use std::string !)
The difficulty with the char* approach would be making sure there's enough room for the inserted characters (since char*'s don't magically resize themselves).
(Also, to insert something inside a string, you can use the insert function since I'm pretty sure += is not what you want here.)
I can't seem to be able to confirm my implementation of this chunk_split because people don't seem to post the results of PHP functions in the manual.
Given this:
Would the results look like this?Code:void chunk_split (std::string & tochunk, std::string::size_type chunklength = 76, const std::string & chunkbit = "\n"); int main () { std::string test(34, 'Z'); std::string retest(test); chunk_split(test, 16, "...\n"); chunk_split(retest); std::cout << test << std::endl; std::cout << retest << std::endl; }
ZZZZZZZZZZZZZZZZ...
ZZZZZZZZZZZZZZZZ...
ZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Hmm, well you should be able to do it with some simple math then.
Figure out how many chunks there are.
Then figure out which chunk your working on: for example, the third chunk would start at 3 * chunklength. But remember to insert the new bit into the right spot by advancing the insertion position by the number of characters you've added to the string so far.
Now that you know where to insert the new bit, it's easy with strings: call insert.
It's harder with C style strings, though, like tabstop said, but the algorithm is not different. What you need to do additionally is make sure there is enough space for new bits and then copy in without overwriting any data. Be careful that you don't terminate the string early, too.