|
-
Jun 26th, 2002, 03:37 AM
#1
Thread Starter
Addicted Member
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!!!!
-
Jun 26th, 2002, 06:16 AM
#2
Thread Starter
Addicted Member
owyeah i forgot, no MFC please....thnx
-
Jun 26th, 2002, 06:22 AM
#3
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 28th, 2002, 01:03 PM
#4
New Member
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?
-
Jun 28th, 2002, 01:14 PM
#5
PowerPoster
Put the header file "string" and also include
using namespace std
-
Jun 29th, 2002, 03:44 AM
#6
Thread Starter
Addicted Member
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.
-
Jun 29th, 2002, 05:15 AM
#7
Monday Morning Lunatic
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
-
Jun 30th, 2002, 12:11 AM
#8
Member
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(nHello, vHello); // 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;
}
-
Jun 30th, 2002, 04:53 AM
#9
Monday Morning Lunatic
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
-
Jun 30th, 2002, 10:33 AM
#10
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.
-
Jun 30th, 2002, 05:26 PM
#11
Monday Morning Lunatic
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
-
Jun 30th, 2002, 11:53 PM
#12
-
Jul 1st, 2002, 09:38 AM
#13
=). 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<char> sep("-;|");
tokenizer tokens(str, sep);
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>
-
Jul 1st, 2002, 11:54 AM
#14
Monday Morning Lunatic
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
-
Jul 10th, 2002, 07:00 PM
#15
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.
-
May 9th, 2003, 11:52 PM
#16
Frenzied Member
(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
-
May 10th, 2003, 05:56 AM
#17
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|