如何从文件读取命令并运行它们?

时间:2020-10-15 17:44:23

标签: arrays c file struct arguments

我有一个command.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:

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

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

一旦您从文件中读取了内容,就可以使用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
}