说我有,上面写着这样的文件:如何从文件中读取命令并在c中执行这些命令?
rotate -45
move 30
凡rotate
和move
是我写的两个功能。 但我正在阅读这些文本文件。
所以,我会怎么能够把这些作为我的程序的命令?
我想使用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*/)
}`
这看起来不正确。我想念你们吗?
对字符串进行标记并将第一个标记作为命令读取(使用strcmp与您建议的已知值进行比较),将第二个标记作为值作为函数的参数。 –
您需要一个解析文本文件并生成c代码的脚本。简而言之,代码生成器。 –