Foros del Web » Programación para mayores de 30 ;) » C/C++ »

how could I obtain the same result like with StringTokenizer in Java [strtok in C++]

Estas en el tema de how could I obtain the same result like with StringTokenizer in Java [strtok in C++] en el foro de C/C++ en Foros del Web. Hello, I want to create an application in C++ where the user pass by argument 3 parameters as the next: p:port h:host m:message so, i ...
  #1 (permalink)  
Antiguo 18/11/2010, 12:39
 
Fecha de Ingreso: octubre-2010
Mensajes: 3
Antigüedad: 14 años
Puntos: 0
how could I obtain the same result like with StringTokenizer in Java [strtok in C++]

Hello,

I want to create an application in C++ where the user pass by argument 3 parameters as the next: p:port h:host m:message

so, i wan't to get this arguments and identificate it.
in Java I know how do this, with StringTokenizer, but in C++ not.
I know that exists a method called "strtok" (<string.h>) that could be use in order to do this.

The problem is that I have not idea of C++. Could you help me with the problem??

The equivalent in Java will be:

String prefix = null;
String sufix = null;
StringTokenizer t = null;


for (int i=0; i<args.length; i++)
{
t = new StringTokenizer (args[i], ":");
prefix = t.nextToken();
sufix = t.nextToken();

....

I want to do the same but in c++.

Thanks.
  #2 (permalink)  
Antiguo 18/11/2010, 15:36
Avatar de nup_  
Fecha de Ingreso: noviembre-2010
Mensajes: 265
Antigüedad: 14 años
Puntos: 32
Respuesta: how could I obtain the same result like with StringTokenizer in Java [strt

Hi:

Take a look at:
http://stackoverflow.com/questions/236129/how-to-split-a-string
http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
http://www.cplusplus.com/reference/clibrary/cstring/strtok/


Nup_
  #3 (permalink)  
Antiguo 19/11/2010, 19:06
 
Fecha de Ingreso: octubre-2010
Ubicación: Edo. de México
Mensajes: 94
Antigüedad: 14 años, 1 mes
Puntos: 9
Respuesta: how could I obtain the same result like with StringTokenizer in Java [strt

Código:
DESCRIPTION
     This interface is obsoleted by strsep(3).

     The strtok() function is used to isolate sequential tokens in a null-ter-
     minated string, str.  These tokens are separated in the string by at
     least one of the characters in sep.  The first time that strtok() is
     called, str should be specified; subsequent calls, wishing to obtain fur-
     ther tokens from the same string, should pass a null pointer instead.
     The separator string, sep, must be supplied each time, and may change
     between calls.

     The implementation will behave as if no library function calls strtok().

     The strtok_r() function is a reentrant version of strtok().  The context
     pointer last must be provided on each call.  The strtok_r() function may
     also be used to nest two parsing loops within one another, as long as
     separate context pointers are used.

     The strtok() and strtok_r() functions return a pointer to the beginning
     of each subsequent token in the string, after replacing the token itself
     with a NUL character.  When no more tokens remain, a null pointer is
     returned.


EXAMPLES
     The following uses strtok_r() to parse two strings using separate con-
     texts:

     char test[80], blah[80];
     char *sep = "\\/:;=-";
     char *word, *phrase, *brkt, *brkb;

     strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");

     for (word = strtok_r(test, sep, &brkt);
          word;
          word = strtok_r(NULL, sep, &brkt))
     {
         strcpy(blah, "blah:blat:blab:blag");

         for (phrase = strtok_r(blah, sep, &brkb);
              phrase;
              phrase = strtok_r(NULL, sep, &brkb))
         {
             printf("So far we're at %s:%s\n", word, phrase);
         }
     }
source: man 3 strtok

everytime you need info about a particular command/function check his man page. If u are under linux/unix/mac just type "man <section> <command/function>" where section is the number of the section you want to check out (section 3 goes for system calls and functions i think).

I hope my answer helped you

Greetings++

Etiquetas: java, result, stringtokenizer
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:02.