2016-10-03 1 views
0

は、ファイルからの引数を解析するための標準的な解決策がありますファイルArgumentFile.txtCでファイルから引数を解析する最も簡単な方法は何ですか?

int a=100; 
int b[3] = { 5, 2, 5 }; 
double c = 0.0014; 

そして、ユーザは、その後

./CodeExecutable ArgumentFile.txt 

を実行して引数を渡すことができcode.c

int main(int argc, char *argv[]) 
{ 
    if (argc > 1) FILE *f = fopen(argv[1], "r"); 
    ParseFile(f); // Set the parameters based on file 
    DoStuff(a,b,c); // Run the process based on the parsed arguments 
} 

メインコードを考えてみましょう?これは、コマンドラインから引き数を解析するgetoptに相当しますか?

+1

fopen(argv [1]、 "r") 'と' Source(argv [1]) 'は簡単です。 'if(argc> 1){FILE * f = fopen(argv [1]、" r ")でなければなりません。 ... ' – chux

+0

コマンドラインから引数を得るための 'モダンな'方法は、 'getopt'または' getopt_long'を使うことです。詳細については、この記事を参照してください:http://stackoverflow.com/questions/1973742/how-to-get-a-value-from-optarg – bruceg

+0

@brucegありがとう!コマンドライン引数を解析するのではなく、ファイルから引数を取り出すのに便利な 'getopt'と同等のものがありますか? –

答えて

3

相当のgetopt()は、正確にはgetopt()です。 getopt()関数は、特にコマンドライン引数を処理しません。コマンドライン引数のスタイルの文字列へのポインタ配列を処理します。

#define MAX_ARGS 256 
#define MAX_FILE_LEN 4096 

int main(int argc, char *argv[]) 
{ 
    if(argc > 1) 
    { 
     FILE *f = fopen(argv[1], "r"); 
     if(f != 0) 
     { 
     char fargs[MAX_FILE_LEN] = "" ; 
     fread(fargs, 1, MAX_FILE_LEN, f) ; 

     // Build fargv from file content 
     char* fargv[MAX_ARGS] ; 
     int fargc = 0 ; 

     fargv[fargc] = strtok(fargs, " \n\r") ; 

     while(fargc < MAX_ARGS && fargv[fargc] != 0) 
     { 
      fargc++ ; 
      fargv[fargc] = strtok(0, "\n\r") ; 
     } 

     // Process fargv using getopt() 
     while((char c = getopt(fargc, fargv, "a:b:c:")) != -1) 
     { 
      switch(c) 
      { 
       ... 
      } 
     } 
     } 
    } 

    ... 

    return 0 ; 
} 

動的にtehの実際のファイルの長さを使用してfargsを割り当てることはおそらく良いですが、上記は一例です。

あなたの入力ファイルは、次のようになります。

-a 100 
-b 5,2,5 
-c 0.0014 

getopt()ループは、必要に応じて引数を処理する必要があります - 例えばsscanf()を使用。

 switch(c) 
     { 
      case 'a' : sscanf(optarg, "%i", a) ; break ; 
      case 'b' : sscanf(optarg, "%i,%i,%i", b[0], b[1], b[2]) ; break ; 
      case 'c' : sscanf(optarg, "%f", c) ; break ; 
     } 

     DoStuff(a, b, c) ; 
1

プレーンなCコードではできません。それを処理するには、プラットフォーム固有のアセンブリ言語コードを記述する必要があります。

あなたの最善の選択肢は、Cプロペラを使用することです。

int main(int argc, char *argv[]) 
{ 
#include "myfile.txt" 
    // Do Stuff 
} 

私はあなたの代わりに直接main()myfile.txtの内容を置くことによって獲得なるか分からない、と述べました。

+0

明らかにあなたは*できます*あなたはそれを行うための関数やコードを書く必要があるかもしれません。 –

+1

私は彼がそれをやろうとはしていないと確信しています。私は思ったことを完全に誤解しています。 – Clifford

+1

@MDXF、 'source(argv [1]);'ビットで判断すると、OPはCプリプロセッサの '#include'文に相当するものを必要とします。あなたが何を考えているのか分かりません。 –

2

私はgetopt()を使用します。より柔軟な例がここにあります。この例は、オプションのoptargと複数のoptargの処理方法を示しています。

#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 

void usage(void) 
{ 
    printf("usage: \n" 
      "This example demonstrates how to add flexibility to the traditional linux getopt()\n" 
      "This help text is printed if the program is executed without arguments\n" 
      "or with an invalid argument configuration.\n" 
      "to view the help file run without arguments or with -h\n" 
      "Oterwise the program accepts two options: -d, -u\n" 
      "-d: can come with 0 or one option argument\n" 
      "-u: can come with one or more option arguments\n" 
      "try this to see the output:\n" 
      "./test -d aaa -u ccc 4 \"quoted multi token string\" -d -u\n"); 
} 

int main(int argc, char **argv) 
{ 
    char data[101]; 
    int opt; 

    memset(data, 0, 101); 
    while ((opt = getopt(argc, argv, "hd:u:t:")) != -1) { 
     switch (opt) { 
     case 'h': 
      usage(); 
      return 0; 
     case 'd': // can accept 0 or 1 parameters 
      if (optarg[0] == '-') { //not an optarg of ours... 
       optind--; 
       printf("option: -d. no tokens (another option follows)\n"); 
       break; 
      } 
      strncpy(data, optarg, 100); 
      printf("option: -d. tokens: %s\n", data); 
      break; 
     case 'u': //can accept one or more parameters ... 
      strncpy(data, optarg, 100); 
      printf("option: -u. tokens: %s", data); 
      //do we have more arguments for 'u'? 
      while(optind <= argc && argv[optind][0] != '-') { 
       strncpy(data, argv[optind], 100); 
       printf(", %s", data); 
       optind++; 
      } 
      printf(".\n"); 
      break; 
     case ':': //this happens if we got an option which expects an arg without any optarg. 
      if(optopt == 'd') {//lets allow a '-d' without its optarg 
       printf("option: -d. no tokens\n"); 
       break; 
      } 
      //otherwise fall through to the default handler 
     default: //covers ':' '?' for missing value, '-h' for help, etc. 
      printf("on error you get: opt=%c. optopt=%c opterr=%d\n", opt, optopt, opterr); 
      return 0; 
     } 
    } 
    return 0; 
} 
関連する問題