Results 1 to 17 of 17

Thread: Split function in C++...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176

    Split function in C++...

    Is there something like a split funcion in VB? I need it to split up an IP adress into an int array.

    THNX!!!!
    JpEgy

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    owyeah i forgot, no MFC please....thnx
    JpEgy

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    New Member
    Join Date
    Jun 2002
    Posts
    10
    i got the following errors when i tried to use that function:

    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : error C2065: 'string' : undeclared identifier
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : error C2297: '&' : illegal, right operand has type 'class ios &(__cdecl *)(class ios &)'
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : error C2065: 'separators' : undeclared identifier
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : error C2065: 'list' : undeclared identifier
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : error C2065: 'words' : undeclared identifier
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : warning C4804: '>' : unsafe use of type 'bool' in operation
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : error C2446: '>' : no conversion from 'int *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(29) : error C2040: '>' : 'int' differs in levels of indirection from 'int *'
    c:\documents and settings\jeffno\desktop\keyboardinput_win32\darkside web server\source.cpp(30) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
    Error executing cl.exe.

    anyone know why?

  5. #5
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Put the header file "string" and also include
    using namespace std
    Baaaaaaaaah

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    Guess....i have wooden shoes, in my free time i sell tulips and I live in a huge windmill...
    Posts
    176
    maybe you could take a look at this code. It takes a string and splits it up by looking for spaces. The last char should be an enter too in order for it to work.

    char * SplitString(char * string, int size, int part)
    {
    int Spaces[255];
    int j=0,h=0,l=0;
    int temp;

    char * ReturnString;

    for(int i = 0; i < size; i++)
    {
    if (string[i] == ' '){
    Spaces[j]=i;
    j++;
    }
    }

    if (part == 0) {
    ReturnString = new char[Spaces[0] + 1];
    for(h = 0; h < Spaces[0]; h++)
    {
    ReturnString[h] = string[h];
    }
    if (h == 0) {return "";}
    ReturnString[h] = '\0';
    }
    else {
    i = Spaces[part] - Spaces[part-1];
    h = 0;
    ReturnString = new char[i];
    for(h = 0; h+1 < i; h++){
    ReturnString[h] = string[Spaces[part-1]+ h + 1];
    }
    if (h == 0) {return "";}
    ReturnString[h] = '\0';
    }
    return ReturnString;
    }


    I made it so there could be some little bugs in it but i haven't found 1 yet.
    JpEgy

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The function that was given earlier is better because it uses C++, not C...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Member TheGuru's Avatar
    Join Date
    Dec 2001
    Posts
    57
    PHP Code:
    #include <iostream.h>
    #include <string.h>

    int main()
    {
        
    char vHello[] = "Hello Internet World";
        
    char token;
        
    char nHello;

        
    // This will copy the string into a new variable.
        // Its a good idea to do this because the actual string
        // gets modified by the string function.

        
    nHello = new char[strlen(vHello) + 1];    // Make room for the null
        
    strcpy(nHellovHello);                    // Copy String
        
        // This is your built in split function in c++ that will split by a char. 
        // This more or less grabs the first part of the string and stores it into token, 
        // then you have call it again to get the rest of it. 

        
    token strtok(nHello" ");
                                
        while (
    token != NULL)    // Do until we have nothing left =P
        
    {
            
    cout << token << endl;
            
    // We using the same string so we dont wanna pass anything.
            
    token strtok(NULL" ");    
        }
        
        
    delete [] nHello;
        return 
    0;


  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Nooooo not strtok...it's evil!
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Zaei
    Guest
    Originally posted by parksie
    strtok is okay as long as you know what you've passed into it, and you don't call it on two different strings before you've finished with them.
    =). I agree, though, strtok is evil =).

    Z.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Where'd I post that?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12

  13. #13
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    =). I agree, though, strtok is evil =).
    Why not use boost::tokenizer?

    Example:

    PHP Code:
    // char_sep_example_1.cpp
    #include <iostream>
    #include <boost/tokenizer.hpp>
    #include <string>

    int main()
    {
        
    std::string str ";;Hello|world||-foo--bar;yow;baz|";
        
    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
        
    boost::char_separator<charsep("-;|");
        
    tokenizer tokens(strsep);
        for (
    tokenizer::iterator tok_iter tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
            
    std::cout << "<" << *tok_iter << "> ";
        
    std::cout << "\n";
        return 
    0;

    The output is:
    Code:
    <Hello> <world> <foo> <bar> <yow> <baz> 

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by twanvl
    Why not use boost::tokenizer?
    I already do, and have done for a long while now

    Well, that is when I don't need the advanced features of my own tokeniser (grouping, etc.) -- once I write the correct functor for the boost one I can move over to that for everything.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You didn't mention that <string.h> is evil.
    The standard headers (iostream, string etc) have NO extension.
    #include <string> !!!!
    #include <iostream> !!!!

    using namespace std; !!!!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    (I know it is an old post... But...)

    Why doesn't this code work when I copy and paste it in VC++ compiler?

    It says boost/tokenizer does not exist..
    Last edited by Tec-Nico; May 9th, 2003 at 11:57 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    www.boost.org

    Download the library from there. You don't actually need to run the build instructions, just add C:\boost_1_30_0 or whatever to VC++'s include directories.

    You only need to build parts of it if you need them, such as the regexps, threads, etc. The tokenisers are all wholly templated.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width