4

私は持っている:

int main(int argc, char **argv) {
   if (argc != 2) {
      printf("Mode of Use: ./copy ex1\n");
      return -1;
   }

   formatDisk(argv);
}

void formatDisk(char **argv) {
   if (argv[1].equals("ex1")) {
       printf("I will format now \n");
   }
}

Cargvで等しいかどうかを確認するにはどうすればよいですか? "ex1"そのための機能はすでにありますか?ありがとう

4

2 に答える 2

20
#include <string.h>
if(!strcmp(argv[1], "ex1")) {
    ...
}
于 2009-04-29T19:04:32.483 に答える
2

文字列を使用し、新しい文字列を動的に割り当てる例を示します。argv [?]のサイズがわからない場合におそらく便利です。

// Make the string with the value you want compared
char testString[] = "-command";

// Make a char pointer, use new to allocate the memory 
//  the size is determined by string length of argv[1]
char * strToTest = new char[ strlen( argv[1] ) ];

// Now we can copy the contents of argv[1] into strToTest as they are equal size
strcpy( strToTest, argv[1] );

// Now strcmp returns True if the two strings match
if (strcmp( testString, strToTest ) {
//do somthing here ...
}
于 2011-08-12T06:56:58.600 に答える