Chunk Split

This is a discussion on Chunk Split within the C++ Programming forums, part of the General Programming Boards category; -delete this thread-...

  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 01:07 PM.

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

    By the way, your implementation of chunk_split is incorrect.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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, 11:15 PM
  2. split file
    By andriss in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 04:47 PM
  3. Split line
    By groorj in forum C Programming
    Replies: 8
    Last Post: 04-24-2005, 01: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, 09:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21