L
linzhenhua1205
I want to parse a string like C program parse the command line into
argc & argv[][].
I hope don't use the array the allocate a fix memory first, and don't
use the memory allocate function like malloc.
who can give me some ideas?
The following is my program, but it has some problem. I hope someone
would correct it.
////////////////////////////
//Test_ConvertArg.c
////////////////////////////
#include <stdio.h>
#include <string.h>
int ConvertArg(char* Str, char* Argv[])
{
int Argc; // Count the argument).
int Count = 0;
int i = 0;
char* StrPtr;
char* TmpStrPtr;
StrPtr = Str;
for(; *StrPtr == ' '
// ignore the whitespace before the
command!
{
++StrPtr;
}
TmpStrPtr = StrPtr;
for (Argc = 0; (*StrPtr) != '\n'; StrPtr++, Count++)
{
if(*StrPtr == ' ')
{
// if exist multi whitespace together,
//argc just count once,and don't continually change argv!
if( *(StrPtr+1) == ' ')
{
Count--;
continue;
}
}
Count--;
// the following setences have problems.
memcpy(Argv[Argc],TmpStrPtr,Count);
#if 0
for(i=0; i <= Count; i++)
{
Argv[Argc] = *TmpStrPtr;
TmpStrPtr++;
}
#endif
TmpStrPtr = StrPtr;
i = 0;
Argc++;
}
}
int main(int argc, char* argv[])
{
char** argv1;
char* str = "";
char** str1 = "test";
char* str2 = " ";
memcpy(str2, str1[0], 4);
printf("%s\n", str2);
// test ConvertStr()
str = " a asdf ";
argv1 = "";
printf("argc = %d, argv0 = \n",ConvertArg(str, argv1));
str = " a asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s, argv2 =
%s\n",ConvertArg(str, argv1), argv1[0], argv1[1], argv1[3]);
return 1;
}
argc & argv[][].
I hope don't use the array the allocate a fix memory first, and don't
use the memory allocate function like malloc.
who can give me some ideas?
The following is my program, but it has some problem. I hope someone
would correct it.
////////////////////////////
//Test_ConvertArg.c
////////////////////////////
#include <stdio.h>
#include <string.h>
int ConvertArg(char* Str, char* Argv[])
{
int Argc; // Count the argument).
int Count = 0;
int i = 0;
char* StrPtr;
char* TmpStrPtr;
StrPtr = Str;
for(; *StrPtr == ' '
command!
{
++StrPtr;
}
TmpStrPtr = StrPtr;
for (Argc = 0; (*StrPtr) != '\n'; StrPtr++, Count++)
{
if(*StrPtr == ' ')
{
// if exist multi whitespace together,
//argc just count once,and don't continually change argv!
if( *(StrPtr+1) == ' ')
{
Count--;
continue;
}
}
Count--;
// the following setences have problems.
memcpy(Argv[Argc],TmpStrPtr,Count);
#if 0
for(i=0; i <= Count; i++)
{
Argv[Argc] = *TmpStrPtr;
TmpStrPtr++;
}
#endif
TmpStrPtr = StrPtr;
i = 0;
Argc++;
}
}
int main(int argc, char* argv[])
{
char** argv1;
char* str = "";
char** str1 = "test";
char* str2 = " ";
memcpy(str2, str1[0], 4);
printf("%s\n", str2);
// test ConvertStr()
str = " a asdf ";
argv1 = "";
printf("argc = %d, argv0 = \n",ConvertArg(str, argv1));
str = " a asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s\n",ConvertArg(str, argv1),
argv1[0], argv1[1]);
str = " asdf asdf";
printf("argc = %d, argv0 = %s ,argv1 = %s, argv2 =
%s\n",ConvertArg(str, argv1), argv1[0], argv1[1], argv1[3]);
return 1;
}