T
Thomas Peterson
Hi,
I just wrote a simple demo-program that should split a char[] in several
pieces. It seems that it is working probably.
But now I like to know if it is good or if there are better ways for
doing what I want.
#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
void split(char *string);
int main(void)
{
char test[30];
strcpy(test, "test.txt -r 123 -t 456 hello");
split(test);
Sleep(20000);
}
void split(char *string)
{
char *sub_string;
while ( (sub_string = strtok(string, " ")) != 0)
{
string = 0;
if(strcmp(sub_string, (char*)"-r") == 0)
{
sub_string = strtok(string, " ");
if(sub_string != 0)
{
printf("sub string -r = '%s'\n", sub_string);
string = 0;
}
}
else if(strcmp(sub_string, (char*)"-t") == 0)
{
string = 0;
sub_string = strtok(string, " ");
if(sub_string != 0)
{
printf("sub string -t = '%s'\n", sub_string);
string = 0;
}
}
}
}
Regards,
Thomas
I just wrote a simple demo-program that should split a char[] in several
pieces. It seems that it is working probably.
But now I like to know if it is good or if there are better ways for
doing what I want.
#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
void split(char *string);
int main(void)
{
char test[30];
strcpy(test, "test.txt -r 123 -t 456 hello");
split(test);
Sleep(20000);
}
void split(char *string)
{
char *sub_string;
while ( (sub_string = strtok(string, " ")) != 0)
{
string = 0;
if(strcmp(sub_string, (char*)"-r") == 0)
{
sub_string = strtok(string, " ");
if(sub_string != 0)
{
printf("sub string -r = '%s'\n", sub_string);
string = 0;
}
}
else if(strcmp(sub_string, (char*)"-t") == 0)
{
string = 0;
sub_string = strtok(string, " ");
if(sub_string != 0)
{
printf("sub string -t = '%s'\n", sub_string);
string = 0;
}
}
}
}
Regards,
Thomas