How to parse a string like C program parse the command line string?

linzhenhua1205_at_163.com
Date: 03/12/05


Date: 11 Mar 2005 21:26:57 -0800

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][i] = *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;
}



Relevant Pages

  • Re: How to parse a string like C program parse the command line string?
    ... >I want to parse a string like C program parse the command line into ... > use the memory allocate function like malloc. ...
    (comp.lang.c)
  • Re: despair
    ... %SYSTEM-F-DEVMOUNT, device is already mounted ... It's a command procedure and should be written ... I'm talking about "someone left the wrong tape in" or "someone else is ... You allocate a resource, you ...
    (comp.os.vms)
  • Re: despair
    ... Say this runs on the SYSTEM account. ... The allocate will fail, but other commands may succeed due to the fact that this account has all mighty privileges. ... Of course, the command would then write the backup to whatever tape was physically in the drive, and it could be someone else's tape. ...
    (comp.os.vms)
  • Re: despair
    ... Please tell me what error from the ALLOCATE command would screw things ... down and said he had ALLOCATED the drive I had my tape on. ... drives and then come down to mount a tape. ...
    (comp.os.vms)
  • Re: generate consecutive numbers
    ... "mark" wrote in message ... > normally allocate numbers in a book from 1 to appox 550. ... > On the second click of the command button I would want to ... > number 1 to the first batch the 2 to the second then ...
    (microsoft.public.excel.newusers)