本文介绍了如何解析像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 intoargc & argv[][].I hope don''t use the array the allocate a fix memory first, and don''tuse the memory allocate function like malloc.who can give me some ideas?The following is my program, but it has some problem. I hope someonewould 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 thecommand!{++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 0for(i=0; i <= Count; i++){Argv[Argc][i] = *TmpStrPtr;TmpStrPtr++;}#endifTmpStrPtr = 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 这篇关于如何解析像C程序解析命令行字符串的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 17:05