c - C语言中如何清除字符缓冲区

标签 c

我正在开发一个 C 程序,该程序保存来自 stdin 的输入并保存到数组中。 我对该数组进行了一些处理,并且我想清除该数组。我也尝试过 malloc 和 free 以及 memset 。他们都没有工作。这是代码

while(1)
{
    printf("? ");
    char *line;
    line = (char *) malloc(MAX_BUFFER);
   // char line[MAX_BUFFER];
    if (!fgets(line, MAX_BUFFER , stdin))
         return 0;
    parse(&cmd, line);  // this function prints the string
    free(line);
}

解析.c

void parse(Command *cmd, char *line)
 {

    char *p = strtok (line, " \n");

    //while loop tokenizes the line into logical parts store them into array called commandsArray
    while (p != NULL)
    {

        // compare the token with output redirection

        if ((!strcmp(p, ">")  || !strcmp(p, ">>") ) &&  ! cmd->isoutputredirected)
        {
             cmd->isoutputredirected = 1;
             cmd->outfileindex  =  cmd->tokenscounter + 1;
             cmd->outputredirectionindex =  cmd->tokenscounter;
        }

        cmd->commandsArray[cmd->tokenscounter++]  = p;
        p = strtok (NULL, " \n");

    }
    //checking if input redirection is found or not

    if (  cmd->isinputredirected == 1)
         cmd->infile =  cmd->commandsArray[ cmd->infileindex];
    else
         cmd->infile = "";

    if (  cmd->isoutputredirected == 1)
        cmd->outfile =  cmd->commandsArray[ cmd->outfileindex];
    else
        cmd->outfile = "";

    if ( cmd->isinputredirected &&  cmd->infile)
        printf("%d: < \'%s\' ",  cmd->commandCounter+1,  cmd->infile );

    int k;

    for ( k = 0; k <  cmd->tokenscounter ; ++k)
    {
         char *token =  cmd->commandsArray[k];

         if(!strcmp(token, cmd->infile)  || !strcmp(token,"<") || !strcmp(token,"<<")    || !strcmp(token, cmd->outfile)  || !strcmp(token,">") ||  !strcmp(token,">>"  )){
             continue;
         }
         else if (!strcmp(token,"|")  || !strcmp(token,">>") || !strcmp(token,">>")  ||  !strcmp(token,"<<") ||   !strcmp(token,">") )
                 printf("%s ", token);
        else
                 printf("\'%s\' ", token);
    }

    if ( cmd->isoutputredirected &&  cmd->outfile)
        printf(" > \'%s\' ",  cmd->outfile );
    printf("\n");
    // clearing the buffer of character and struct
    memset(&cmd, 0, sizeof(cmd));
    memset(line, 0, 1024); 

当我运行时,我得到这样的输出

? hello world
  hello world
? hello world
  hello world hello world

我的问题是缓冲区没有清除。

最佳答案

我想我看到了问题所在,尽管代码相当不透明。线路

cmd->commandsArray[cmd->tokenscounter++]  = p;

是将行的内容存储到命令中的内容。它将其存储在数组位置 cmd->tokenscounter 中。但是你永远不会将 cmd->tokenscounter 归零,所以在第一次调用之后,你有

cmd->commandsArray[0] == "hello"
cmd->commandsArray[1] == "world"
cmd->tokenscounter == 2

在你第二次打电话时写

cmd->commandsArray[2] = "hello"
cmd->commandsArray[3] = "world"
cmd->tokenscounter == 4

因此,当您第二次打印时,您将打印所有四个值。

关于c - C语言中如何清除字符缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50519697/

相关文章:

c - gcc 选项 : pkg-config --libs --cflags gtk+-3. 0

c - 在套接字上设置 TCP ECN (C Linux)

c - 使用 time.h 的奇怪 CLOCKS_PER_SEC 值

c - scanf: "%[^\n]"跳过第二个输入但 "%[^\n]"没有。为什么?

c++ - 我如何知道 IMAGE_THUNK_DATA 数组何时终止?

C 编程 nan 输出

c - 从假定相同尺寸的类型进行类型转换时发出警告

c - 内联-c : "` Type` cannot be marshalled in a foreign call"

c - 指针数组与多维数组

c - 链接器:将除两个之外的所有函数移动到特定的内存区域