【问题标题】:How do I read commands from a file and run them?如何从文件中读取命令并运行它们?
【发布时间】:2020-10-15 17:44:23
【问题描述】:

我有一个 commands.txt 文件,内容如下:

LIST1
LIST2
LIST3
ASSIGN C6650 SENG101
ADD 1 SENG101
LIST4 1
LIST5 C6650
LIST6 SENG101
LIST7 15
DROP 1 SENG101
LIST4 1
LIST6 SENG101
LIST7 15

我将此文件作为参数发送给我的程序。目标是:阅读 commands.txt 文件并在其中执行命令。 (每一行都是命令)

LIST1,LIST2,LIST3,ASSIGN,ADD,LIST4,LIST5,LIST6,LIST7,DROP 是我要编写的函数。但我正在从文本文件中读取这些内容。

那么,我怎样才能将这些用作我的程序的命令?

我使用了 strcmp。所以我可以将我读到的字符串与我所知道的可能的命令进行比较。然后,如果它们匹配,我想调用请求的函数。

如果它们都是一个单词,我可以进行所有比较,但例如,我如何执行ASSIGN C6650 SENG101 命令?即使 ASSIGN 保持不变,C6650SENG101 也可能会发生变化。在此代码中,C6650 是教师代码,SENG101 是课程代码。

我真的很想看一些示例代码来帮助理解。 目前我写的代码:

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

#pragma warning (disable:4996)
#define MAXCHAR 100

struct students {
    int studentNumber;
    char studentSurname[50];
    char studentName[50];

};
struct courses {
    char courseCode[7];
    char courseName[50];
    int courseCredit;

};
struct lecturers {
    char regNumber[5];
    char regSurname[50];
    char regName[50];

};

/*
        PLEASE SEND ARGUMENTS IN THE FOLLOWING ORDER TO THE PROGRAM
        Ex: C:>DBMS students.txt lecturers.txt courses.txt commands.txt output.txt
*/

int main(int argc, char* argv[])
{
    char com1[] = "LIST1";
    char com2[] = "LIST2";
    char com3[] = "LIST3";
    char com4[] = "LIST4";
    char com5[] = "LIST5";
    char com6[] = "LIST6";
    char com7[] = "LIST7";
    char com8[] = "ASSIGN";
    char com9[] = "ADD";
    char com0[] = "DROP";

    char readed[MAXCHAR];

    FILE* fstudent = fopen(argv[1], "r"); // "r" for read
    FILE* flecturers = fopen(argv[2], "r"); // "r" for read
    FILE* fcourses= fopen(argv[3], "r"); // "r" for read
    FILE* fcommands = fopen(argv[4], "r"); // "r" for read
    FILE* foutput = fopen(argv[5], "a+"); // "a+" for append

    
    while (fgets(readed, MAXCHAR, fcommands) != NULL) {
        printf("%s", readed);                               //I DONT KNOW SHOULD WE TAKE THEM LINE BY
        readed[strcspn(readed, "\n")] = 0;                  //LINE OR WORD BY WORD? THIS IS THE LINE
                                                            //BY LINE VERSION.
        if (0 == strcmp(com1, readed)) {
            //LIST1
            printf("----------------LIST1------------\n");
        }
        if (0 == strcmp(com2, readed)) {
            //LIST2
            printf("----------------LIST2------------\n");
        }
        if (0 == strcmp(com3, readed)) {
            //LIST3
            printf("----------------LIST3------------\n");
        }
        if (0 == strcmp(com4, readed)) {
            //LIST4
            printf("----------------LIST4------------\n");      //The required function will be called 
                                                                //here. I used printf to just try it out.
        }
        if (0 == strcmp(com5, readed)) {
            //LIST5
            printf("----------------LIST5------------\n");
        }
        if (0 == strcmp(com6, readed)) {
            //LIST6
            printf("----------------LIST6------------\n");
        }
        if (0 == strcmp(com7, readed)) {
            //LIST7
            printf("----------------LIST7------------\n");
        }
        if (0 == strcmp(com8, readed)) {
            //ASSIGN
            printf("----------------LIST8------------\n");
        }
        if (0 == strcmp(com9, readed)) {
            //ADD
            printf("----------------LIST9------------\n");
        }
        if (0 == strcmp(com0, readed)) {
            //DROP
            printf("----------------LIST0------------\n");
        }       
    }
    

    /*
     while (fscanf(fcommands, "%s", readed) != EOF)
     {                                                        //I DONT KNOW SHOULD WE TAKE THEM LINE BY 
         printf("%s\n", &readed);                             //LINE OR WORD BY WORD? THIS IS THE WORD 
                                                              //BY WORD VERSION.
     }
    */   
    
    return 0;
} 

当我用发送参数运行这个程序时,如图所示,if 行在LIST3 之后不起作用,因为readed 变量包含多个单词。

Picture:

那么我该如何解决这些问题呢?如果您能帮助我,我将不胜感激。

【问题讨论】:

  • 您可以使用strtok() 提取每个“单词”。将第一个标记与ASSIGN 匹配后,然后提取其操作数/参数。 MSVC man page 给出了示例代码,它还处理 fgets() 读取的尾随换行符 - 它不需要显式删除。

标签: arrays c file struct arguments


【解决方案1】:

从文件中读取readed 后,您可以使用sscanf

类似:

if (sscanf(readed, "%s %s %s", word1, word2, word3) == 3)
{
    // word1 is now ASSIGN, ADD  or DROP - use strcmp to find which
    // the arguments is in word2 and word3
}
else if (sscanf(readed, "%s %s", word1, word2) == 2)
{
    // word1 is now LIST4, LIST5, ...  - use strcmp to find which
    // the argument is in word2
}
else if (sscanf(readed, "%s", word1) == 1)
{
    // word1 is now LIST1, LIST2, ...  - use strcmp to find which
}
else
{
    // ERROR
}

【讨论】:

  • 谢谢大家。我会用它。如果我以后遇到问题,我可以在这个答案下写吗?或者,如果您使用的是 discord-whatsappp-instagram 或 skype,以便我可以直接与您联系,我可以得到吗?
  • @GençYazılımcı 如果您对上述内容有任何疑问,可以在这里写评论。
  • 如何将students.txt文件中的students定义到名为students的结构中。 struct 和 students.txt 看起来像这样:imgur.com/a/KKGWKdC
【解决方案2】:

从文件中读取后,您可以使用 sscanf。

类似:

if (sscanf(readed, "%s %s %s", word1, word2, word3) == 3)
{
    // word1 is now ASSIGN, ADD  or DROP - use strcmp to find which
    // the arguments is in word2 and word3
}
else if (sscanf(readed, "%s %s", word1, word2) == 2)
{
    // word1 is now LIST4, LIST5, ...  - use strcmp to find which
    // the argument is in word2
}
else if (sscanf(readed, "%s", word1) == 1)
{
    // word1 is now LIST1, LIST2, ...  - use strcmp to find which
}
else
{
//Error message
    // ERROR
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2022-11-19
    • 2013-08-21
    • 2020-01-16
    • 2016-05-09
    相关资源
    最近更新 更多