2012-10-01 109 views
2

說我有,上面寫着這樣的文件:如何從文件中讀取命令並在c中執行這些命令?

rotate -45 
move 30 

rotatemove是我寫的兩個功能。 但我正在閱讀這些文本文件。

所以,我會怎麼能夠把這些作爲我的程序的命令?

我想使用strcmp,例如。所以我會比較我讀的字符串和我所知道的可能的命令。然後,如果它們匹配,我想調用請求的函數。

我真的很想看到一些示例代碼,以幫助理解。

感謝您的提示。 所以使用brunobeltran0的第一種方法,我會做這些:

 char*next_command; 
     char* get_next_command;g = fopen("b", "r");/*b is the name of the file*/ 

     while(!feof(g)) 
     { 
     get_next_command=fgetc(g); 

     next_command = strtok(get_next_command, " "); 

     while (next_command != NULL) { 

     printf("%s\n", next_command); 

     next_command = strtok(NULL, " ");  

     if (!strcmp(next_command, "rotate")) 
     { 

     rotate (/*how would I get the number to be in here*/) 


     }` 

這看起來不正確。我想念你們嗎?

+0

對字符串進行標記並將第一個標記作爲命令讀取(使用strcmp與您建議的已知值進行比較),將第二個標記作爲值作爲函數的參數。 –

+0

您需要一個解析文本文件並生成c代碼的腳本。簡而言之,代碼生成器。 –

回答

1

如下我會做到這一點:

  • 職能將被定義爲採取可變數量的參數,除非他們都有着不同的名稱相同的函數簽名。
  • 將函數名稱的映射定義爲指向函數定義的指針的字符串。
  • 每一行都被讀取和解析,函數名將使用上面的映射圖解析爲函數指針,並用參數調用。

HTH。

2

我會假設你的意思是你想編寫一個程序,給定的輸入文件的完整它能理解的命令,從所述輸入文件中讀取並執行這些命令。 根據您擁有的命令數量以及您對這些函數進入時格式化的瞭解,周圍的代碼可能會有明顯的不同,但在一般情況下,邏輯將是沿着(忽略內存管理)

char *next_command = get_next_command(...); // reading the commands is really specific to the input you expect 
if (!strcmp(next_command, "some_command")) 
{ 
    void *param_arr[PARAM_CNT_FOR_SOME_COMMAND] = get_params_for_some_command(); 
    some_command(param_arr[0], param_arr[1], param_arr[2]); // assume some_command takes 3 arguments 
} 
else if (!strcmp(next_command, "some_other_command")) 
... 

例如,如果你想旋轉-45,

char *next_command = get_next_command(...); // reading the commands is really specific to the input you expect 
if (!strcmp(next_command, "rotate")) 
{ 
    void *param_arr[1] = get_rotation_angle(); 
    rotate((int *)param_arr[0]); // assume some_command takes 3 arguments 
} 

應該工作。

如果你有一個地圖,然後從可能的輸入命令映射到各自的職責,使功能從文件本身讀取來尋找它的參數很可能會更有效。

例如:

char *next_command = get_next_command(file_pointer); 
(*get_func_pointer(next_command))(file_pointer); // where get_func_pointer is a function that 
               // returns the function pointer assoc. with 'next_command' 
/* somewhere else in the code */ 
void func_returned_by_get_func_pointer(FILE *fp) 
{ 
    read_params_from(fp); 
    do_everything_as_usual(); 
} 
+0

問題得到了更新。請看一看。 –

0

嘗試去耦儘可能的功能解析器/調度代碼。這顯示了調度員的一個例子,將您的樣品情況下工作:

typedef int (*Command)(double); 
struct CommandEntry { 
    const char *name; 
    Command function; 
}; 

int dispatch_commands(struct CommandEntry *commands, int num_commands, 
         const char *name, double param) 
{ 
    /* loop over the arguments, look for a CommandEntry with a matching name, 
     convert its parameter and then call it */ 
    int i; 
    for (i = 0; i < num_commands; ++i) { 
     if (!strcmp(commands[i].name, arg)) 
      return commands[i].*function(param); 
    } 
    return -1; 
} 

struct CommandEntry commands[] = { 
    {"rotate", &my_rotate_func}, 
    {"move", &my_translate_func} 
}; 

這是假設所有的命令採取double類型的一個參數,當然......如果你所有的命令都是這個同質,可以使這行得通。否則,您需要指定每個CommandEntry中的參數個數(並傳遞一個數組,因爲函數類型仍然需要相同),或者爲每個命令函數指定整個傳入的令牌流,並允許它消耗盡可能多的令牌。