如何解析像C程序解析命令行字符串的字符串? [英] How to parse a string like C program parse the command line string?

查看:70
本文介绍了如何解析像C程序解析命令行字符串的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析像C程序一样的字符串解析命令行到

argc& argv [] []。

我希望不要先使用数组分配修复内存,而不要使用内存分配函数


谁可以给我一些想法?

以下是我的程序,但它有一些问题。我希望有人

会纠正它。

/////////////////////////// /

//Test_ConvertArg.c

//////////////////////////////

#include< stdio.h>

#include< string.h>


int ConvertArg(char * Str ,char * Argv [])

{

int Argc; //计算参数。

int Count = 0;

int i = 0;

char * StrPtr;

char * TmpStrPtr;

StrPtr = Str;


for(; * StrPtr ==''';;)//忽略之前的空格

命令!

{

++ StrPtr;

}


TmpStrPtr = StrPtr;


for(Argc = 0;(* StrPtr)!=''\ n''; StrPtr ++,Count ++)

{


if(* StrPtr =='''')

{

//如果存在多个空格,

// argc算一次,不要一直改变argv!

if(*(StrPtr + 1)=='''')br / >
{

伯爵 - ;

继续;

}

}


计数 - ;

//以下句子有问题。

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);

//测试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]);

返回1;

}

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;
}

推荐答案



< li ************ @ 163.com> schreef in bericht

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

<li************@163.com> schreef in bericht
news:11**********************@f14g2000cwb.googlegr oups.com...
我想解析像C程序一样的字符串解析命令行到
argc& argv [] []。
我希望不要先使用数组分配修复内存,也不要像使用malloc那样使用内存分配功能。
谁能给出我有些想法?
以下是我的程序,但它有一些问题。我希望有人能够纠正它。
////////////////////////////
// Test_ConvertArg .c
////////////////////////////
#include< stdio.h>
#include< string.h>

int ConvertArg(char * Str,char * Argv [])
{Argc; //计算参数。
int Count = 0;
int i = 0;
char * StrPtr;
char * TmpStrPtr;
StrPtr = Str; < (; * StrPtr =='''';)//忽略
命令之前的空格!
{
++ StrPtr;
}

TmpStrPtr = StrPtr;

for(Argc = 0;(* StrPtr)!=''\ n''; StrPtr ++,Count ++)
{

如果(* StrPtr =='''')
{
//如果存在多个空格,
// argc只计算一次,并且不要'不断改变argv!
if(*(StrPtr + 1)=='''')
{
Count--;
继续;
}
}

数 - >
//以下句子有问题。
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 asff" ;;
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]);
返回1;
}
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;
}




使用getopt函数


Johan



use the getopt function

Johan


但我的程序不想在systemV Unix下运行

喜欢它。

那不是getopt系统调用

but my program is not want to run under the systemV Unix or something
like it.
That is not getopt system call


我重写转换函数如下:

/// ///////////////////

/////////////////////// ////////////////////////////////////////////////// //////

///////

//目的:此函数将Str转换为[int argc]& [char *

argv []],

//与C main函数的参数相同。

//

// ARGUMENT:Str包含命令,开关和存档文件。

// RETURN:参数的数字。

//信息:1.skip领先的空白区域和表空间!

// 2.如果出现''\ 0''休息;

// 3.Put the保持字符串进入Argv [Argc],记住Argv []

//是一个字符串指针。如果你在字符串中输入''\'''意味着

//你填充了Argv [] []。

// 4.检查'' '''''\\'''''\\'n''

// 5.填充字符串''\ n''

// 6 。将字符串指针改为下一个参数。

// TestStatus:UNDO

/////////////////// ////////////////////////////////////////////////// //////////

///////

int ConvertArg(char * Str)

{

int Argc; //计算参数。

char * Argv [MAXARGC]; //保存解析结果。

char * StrPtr,* CurrentPtr;

StrPtr = Str;


for(Argc = 0; Argc< MAXARGC; Argc ++)//初始化Argv。

{

Argv [Argc] = NULLCHAR;

}

for(Argc = 0; Argc< MAXARGC&&(* StrPtr)!=''\ 0'';)

{

//跳过前导空格和表空间!

while(* StrPtr ==''''||| * StrPtr ==''\ t'')

{

StrPtr ++;

}


//当这只是字符串中的空格时,这个实例就会发生!

if((* StrPtr)==''\''')//如果发生了中断char''\ 0''

{

休息;

}


Argv [Argc ++] = StrPtr; //令牌的开头。


//查找空格或制表符。如果没有,那么我们已经找到了

的最后一个令牌。

for(CurrentPtr = StrPtr; * CurrentPtr; CurrentPtr ++)

{

if(* CurrentPtr ==''''|| * CurrentPtr ==''\ t'')

{

break ;

}

}


if(* CurrentPtr!=''\''')

{

* CurrentPtr ++ =''\''';

}

StrPtr = CurrentPtr;

}


//空命令行

if(Argc< 1)

{

Argc = 1;

Argv [0] ="" ;;

}


返回Argc;

}

I rewrite the Convert function like following:
//////////////////////
///////////////////////////////////////////////////////////////////////////////
///////
//PURPOSE : This function converts the Str into [int argc] & [char*
argv[]],
// which are the same as the argument of the C main function.
//
//ARGUMENT : Str contains the command , switch and archive file.
//RETURN : the number in of the argument.
//INFO : 1.skip leading white space and table space!
// 2.if occur the ''\0'' break;
// 3.Put the remain string into the Argv[Argc], be remember Argv[]
// is a string Pointer. if you file ''\0'' in string it means that
// you fill the Argv[][].
// 4.check the '' '' ''\t'' ''\n''
// 5.fill the string by ''\n''
// 6.change the string pointer to the next argument.
//TestStatus: UNDO
///////////////////////////////////////////////////////////////////////////////
///////
int ConvertArg(char* Str)
{
int Argc; // Count the argument).
char* Argv[MAXARGC]; // to hold the parse result.
char *StrPtr, *CurrentPtr;
StrPtr = Str;

for(Argc = 0;Argc < MAXARGC;Argc++) // init the Argv.
{
Argv[Argc] = NULLCHAR;
}
for(Argc = 0;Argc < MAXARGC && (*StrPtr) != ''\0'';)
{
// Skip leading white space and table space!
while(*StrPtr == '' '' || *StrPtr == ''\t'')
{
StrPtr++;
}

// When that is only space in the string this instance will happen!
if((*StrPtr) == ''\0'') // break if occur the char ''\0''
{
break;
}

Argv[Argc++] = StrPtr; // Beginning of token.

// Find space or tab. If not present then we''ve already found the
last token.
for (CurrentPtr = StrPtr; *CurrentPtr; CurrentPtr++)
{
if (*CurrentPtr == '' '' || *CurrentPtr == ''\t'')
{
break;
}
}

if (*CurrentPtr != ''\0'')
{
*CurrentPtr++ = ''\0'';
}
StrPtr = CurrentPtr;
}

// empty command line
if (Argc < 1)
{
Argc = 1;
Argv[0] = "";
}

return Argc;
}


这篇关于如何解析像C程序解析命令行字符串的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆