2012-10-01 115 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中的参数个数(并传递一个数组,因为函数类型仍然需要相同),或者为每个命令函数指定整个传入的令牌流,并允许它消耗尽可能多的令牌。