Thread: Chunk Split

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Please delete this thread

    -delete this thread-
    Last edited by bobbelPoP; 08-12-2008 at 12:07 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Though thing is, is this possible with char/char*?
    Of course.

    By the way, your implementation of chunk_split is incorrect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    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 !)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    Of course.

    By the way, your implementation of chunk_split is incorrect.
    Oh stop teasing me!

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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:
    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;
    }
    Would the results look like this?

    ZZZZZZZZZZZZZZZZ...
    ZZZZZZZZZZZZZZZZ...
    ZZ
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by citizen View Post
    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:
    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;
    }
    Would the results look like this?

    ZZZZZZZZZZZZZZZZ...
    ZZZZZZZZZZZZZZZZ...
    ZZ
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    chunk_split in php returns the string back with the chunk bit returns after every character by the amount of chunk length.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-09-2009, 10:15 PM
  2. split file
    By andriss in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 03:47 PM
  3. Split line
    By groorj in forum C Programming
    Replies: 8
    Last Post: 04-24-2005, 12:05 PM
  4. Split int variable
    By Jas11 in forum C++ Programming
    Replies: 4
    Last Post: 03-28-2005, 05:06 PM
  5. Split function
    By fkheng in forum C Programming
    Replies: 7
    Last Post: 07-08-2003, 08:26 PM