如何将仅4“>”的输出输出到stdout

时间:2019-03-11 09:32:58

标签: c bash printing

我要制作一个背包程序,该程序可以输入数字并存储其计数,并具有类似UI的界面,用户可以在其中输入命令,例如从文件保存和加载。尽管所有这些工作都很好,并且在下面提供了此类代码,但我很难为该程序生成预期的测试用例输出。

我想知道自己在做错什么,为什么不输出"> "呢?如果我从其他部分删除fprintf(stdout, "> ");行,则有3个"> "打印到标准输出。我需要能够打印其中的4张纸,但是我对如何做到这一点感到困惑。我正在使用travis-ci.com来测试该测试用例,而这正是它向我显示失败的错误的地方。下面是完整的代码和测试用例。

当前输出

    Your stderr:
Error: Bad item "abc"
Error: Bad item "xyz"
unknown command: d

所需的输出

    Expected stdout:
> > > > 
    Expected stderr:
Error: Bad item "abc"
Error: Bad item "xyz"
Unknown command: d

背包测试箱4.sh

#!/bin/bash

echo "Test case 4"
echo "Bad commands"
echo "a abc"
echo "r xyz"
echo "d"
echo -e "Error: Bad item \"abc\"\nError: Bad item \"xyz\"\nUnknown command: d" > expectedstderr.tmp
echo -ne "> > > > " > expectedstdout.tmp
echo -e "a abc\nr xyz\nd" | ./knapsack-shell 1>actualstdout.tmp 2>actualstderr.tmp &
sleep 1
killall -9 knapsack-shell

echo "  Expected stdout:"
cat expectedstdout.tmp
echo ""
echo "  Expected stderr:"
cat expectedstderr.tmp

stdoutres=`diff expectedstdout.tmp actualstdout.tmp | wc -l`
stderrres=`diff expectedstderr.tmp actualstderr.tmp | wc -l`
if [ $stdoutres -eq 0 ] && [ $stderrres -eq 0 ]; then
    echo "Test passed"
else 
    if [ $stdoutres -ne 0 ]; then
        echo "  Your stdout:"
        cat actualstdout.tmp
        echo ""
    fi
    if [ $stderrres -ne 0 ]; then
        echo "  Your stderr:"
        cat actualstderr.tmp
    fi
    rm *.tmp
    exit 1
fi
rm *.tmp

背包壳

#include <stdio.h>
#include "knapsack.c"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFFER_SIZE 1000
void removeSpaces(char *str1)
{
    char *str2;
    str2=str1;
    while (*str2==' ') str2++;
    if (str2!=str1) memmove(str1,str2,strlen(str2)+1);
}

int main()
{
    listitemptr k2 = NULL;
    char input[100];
    char command;
    int i, returnval = 0;
    char filename[250];
    char userfile[260];
    char buffer[BUFFER_SIZE];
    char *error;
    int totalRead = 0;
    fprintf(stdout, "> ");  
   for (;;) {
       FILE* pf = NULL;

        if (!fgets(input, sizeof input, stdin))
            break;
        i = strspn(input, " \t\n");  /* skip blanks */
        command = input[i++];
        if (command == 'q' && input[i] == '\n')
            break;
        if (command == 'p') {
            KnapsackPrint(&k2);
    fprintf(stdout, "> ");
            continue;
        }
        if (command == 'a') {
            int item;
            if (sscanf(input + i, "%i", &item) != 1) {
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");  
                fprintf(stderr, "Error: Bad item \"%s\"\n", strtok(input + i, "\n"));

                continue;
            }
            KnapsackAdd(&k2, item);
            KnapsackPrint(&k2);
    fprintf(stdout, "> ");
            continue;
        }
        if (command == 'r') {

            int item;
            if (sscanf(input + i, "%i", &item) != 1) {
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");
                fprintf(stderr, "Error: Bad item \"%s\"\n", strtok(input + i, "\n"));
                continue;
            }

            if(KnapsackRemove(&k2, item) == -1){
                fprintf(stderr, "Error: item %d does not exist in knapsack\n", item);
    fprintf(stdout, "> ");
                continue;
            }else{
            KnapsackPrint(&k2);
        fprintf(stdout, "> ");
            continue;
            }

        }
        if(command == 's'){
            if(( pf = fopen(input + i, "a")) != NULL){
                error = input + i;
                removeSpaces(error);
                KnapsackPrint2(&k2, pf);
                fprintf(stdout, "stored in file \"%s\"\n", strtok(input+i, "\n"));
    fprintf(stdout, "> ");
            }else{
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");
                fprintf(stdout, "Error: unable to save file \"%s\"\n", strtok(input+i, "\n"));
            }
            continue;
        }
        if(command == 'l'){
            if((pf = fopen(input + i, "r")) != NULL){
                error = input + i;
                removeSpaces(error);
                fprintf(stdout, "loaded from file \"%s\"\n", strtok(input+i, "\n"));
                fprintf(stdout, "knapsack:");
                while(fgets(buffer, BUFFER_SIZE, pf) != NULL){
                    totalRead = strlen(buffer);

                    buffer[totalRead -1] = buffer[totalRead -1] == '\n' ? '\0' : buffer[totalRead -1];

                    printf("%s\n", buffer);
            fprintf(stdout, "> ");
                }

            }else {
                error = input + i;
                removeSpaces(error);
        fprintf(stdout, "> ");
                fprintf(stderr, "Error: unable to load file \"%s\"\n", strtok(input+i, "\n"));

            }
            continue;
        }
        else{

        fprintf(stderr, "unknown command: %s", input);
    fprintf(stdout, "> ");          
        }


     }
    return returnval;
}

0 个答案:

没有答案