【发布时间】: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 保持不变,C6650 和 SENG101 也可能会发生变化。在此代码中,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 变量包含多个单词。
那么我该如何解决这些问题呢?如果您能帮助我,我将不胜感激。
【问题讨论】:
-
您可以使用
strtok()提取每个“单词”。将第一个标记与ASSIGN匹配后,然后提取其操作数/参数。 MSVC man page 给出了示例代码,它还处理fgets()读取的尾随换行符 - 它不需要显式删除。
标签: arrays c file struct arguments