繁体   English   中英

使用 FILE* 和结构数组在 C 中实现责任链

[英]Implementing chain of responisiblity in C with FILE* and array of structs

我的目标是创建一个从 CMD 直接写入文本文件的程序。

如果输入字符串是以下字符串之一: -exit -count -remove

它将执行退出程序/计算文件中的行数/删除文件的任务。

我写了一个代码,在我的脑海里基本上看起来“不错”,但在纸上(在显示器上)它很糟糕。

main function工作 100%(没有调用InputCmp function 。我不知道为什么我不能真正将这些结构相互连接起来。

我想创建一个按以下方式工作的责任链:假设用户编写字符串: "-exit" ,但 exit 是 3 结构(数组中的索引 2)。

所以我希望将字符串发送到 function (我写的称为InputCmp ),它将检查strcmp与第一个结构内的字符串,即-remove 如果不匹配,它将与数组中的下一个结构进行比较。 直到找到其中包含确切字符串的第三个结构,然后它将运行 function 退出。

但是,这里的主要问题是以某种方式将FILE*从 function 转移到 function。 我的意思是我需要将它从main传输到InputCmp并从InputCmp到每个函数,因为countremove需要文件才能操作。

我刚刚迷路了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define BUFF_SIZE 1024

/******************************************************/

struct processor
{
        char *task;
        void (*InputCmp)(char*, size_t);
        void (*RunTask)(char*);
};
 
struct processor handlers[3];

handlers[0].task = "-remove";
handlers[1].task = "-count";
handlers[2].task = "-exit";
handlers[0].RunTask = RemoveFile;
handlers[1].RunTask = CountLines;
handlers[2].RunTask = ExitProgram;

/******************************************************/

int RemoveFile(char *string) /* needs somehow to get filename */
{ 

   if (remove(filename) == 0) 
      printf("Deleted successfully"); 
   else
      printf("Unable to delete the file"); 
  
   return 0; 
   
} 

/******************************************************/

void CountLines(char *string) /* needs somehow to get filename */
{
    FILE *fileptr;

    int count_lines = 0;

    char chr;

    fileptr = fopen(filename, "r");

    chr = getc(fileptr);

    while (chr != EOF)

    {

        if (chr == 'n')

        {

            count_lines = count_lines + 1;

        }

        chr = getc(fileptr);

    }

    fclose(fileptr); //close file.

    printf("Lines: %d",count_lines);

}

/******************************************************/

void ExitProgram(char *string)
{
    exit(1);
}


/******************************************************/

 int InputCmp(char *string, size_t index)
{
    assert(string);
    
    if (0 == strcmp(string, handlers[index].task))
    {
        return handlers[index].RunTask(string);
    }
    return handlers[index+1].InputCmp(string,index+1);
} 

/******************************************************/

int is_file_exists(char *file_name)
{
    
    FILE *file;
    if ((file = fopen(file_name,"r"))!=NULL)    
        {
            /* file exists */
            fclose(file);
            return 1;
        }    
    else  
        {
            /*File not found, no memory leak since 'file' == NULL
            fclose(file) would cause an error */
            return 0;
        }
        
}

/******************************************************/

int main(int argc, char **argv)
{
    char c;
    FILE *file;
    char buffer[BUFF_SIZE];

    if (argc >= 2)
    {
         if (is_file_exists(argv[1]))
         {
             file = fopen(argv[1], "a");
         }
         else
         {
             return 0;
         }
    }
    else
    {
         file = fopen("file.txt", "a");
    }

    while(1)
    {
    size_t i = 0;
    memset(buffer, 0, BUFF_SIZE);
    while ((c = getchar()) != '\n' && i < BUFF_SIZE)
    buffer[i++] = c;
    InputCmp(buffer, 0);
        buffer[i] = '\n';
        fputs(buffer, file); 
    }

    fclose(file);
    return 0;
}

不幸的是,您无法从 FILE object 派生文件名

要解决这个问题,您首先必须移动这些:

handlers[0].RunTask = RemoveFile;
handlers[1].RunTask = CountLines;
handlers[2].RunTask = ExitProgram;

在主要的 function 中,并纠正 function 中的小错误(如 \n 不是 n,\n 允许换行):

int RemoveFile(char *filename)
{
   if (remove(filename) == 0)
      printf("Deleted successfully\n");
   else
      printf("Unable to delete the file\n");
   return 0;
}

void (*RunTask)(char*, void*);

/******************************************************/

void CountLines(char *filename)
{
    FILE *file = fopen(filename, "r");
    int count_lines = 0;
    char chr;

    chr = getc(file);

    while (chr != EOF)
    {
        if (chr == '\n')
        {
            count_lines = count_lines + 1;
        }
        chr = getc(file);
    }
    fclose(file); //close file.
    printf("Lines: %d\n",count_lines);
}

/******************************************************/

void ExitProgram(char *s)
{
    (void)s; // to allow compilation with -Wall -Werror
    exit(EXIT_SUCCESS);
}

在您的主要功能中,您可以调用以下功能:

handlers[0].RunTask("file.txt");
handlers[1].RunTask("file.txt");
handlers[2].RunTask("file.txt");

或者:

CountLines("file.txt");
RemoveFile("file.txt");
ExitProgram("");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM