从STDIN

时间:2016-08-10 12:38:20

标签: c overflow

我是C编程的新手,而且我一直在编写一个从STDIN获取输入的虚拟计算机程序,输入基本上代表了让虚拟计算机执行一定数量检查的倍数的命令 - 只是简单的东西。基本上当我第一次编写这个程序时,我正在使用文件指针从文件中读取输入,但是当我将流程切换到STDIN时,它开始变得很奇怪。

这个STDIN的有趣之处在于它是一个文件流重定向,所以我仍然在命令行参数中提供一个文件,但是因为我使用的编码平台有一个允许文件重定向的命令,而不必实现一个实际的文件指针,让我感到困惑。

我开始遇到溢出错误,当我以前有命令行参数中提供的程序的文件指针时没有发生,我不知道为什么因为我刚刚将fscanf切换为gets,显然删除了文件指针的东西。如果有人可以向我指出问题可能是什么,我会非常感激,这是我从以下方面获得溢出错误的功能:

int compile ( int memory [], int* instructionCounter , int* instructionRegister ,int*operationCode ,int* operand){
    char s[800]; /* The buffer */
    char temp[800];
    *instructionRegister=0;
    *operationCode=0;
    while(gets(temp)){ /*Reads data line by line then stores the integer      returned by fscanf to instructionRegister pointer so that I can check for formating */

    *instructionRegister=sscanf(temp,"%2d %s %d",operationCode,s,operand); 
       printf("%d %s %d\n",*operationCode,s,*operand);
        if((*instructionRegister) ==3 ){ /*Checks for improper format by


             comparing the current instructionRegister count to 3, returns improper format otherwise */
            if(*operand >9999|| *operand <0){  /* Checks for word overflow in compiler, makes sure that digits do not exceed 9999 */
                printf("attempts to place a word in memory that is larger than 4 digits, or attempted to pass in a negative value\n ");
                exit(0);
            }
            /*Compares the string section of the code and checks if it matches the following words and then it converts it to it's 4 digit value by adding into it the operand */
            if(strcmp(s,"READ") == 0) {
                memory[*operationCode] = 10 * 100 + *operand;
            }
            else if(strcmp(s,"WRIT") == 0) {
                memory [*operationCode] = 11 * 100 + *operand;
            }
            else if(strcmp(s,"LOAD") ==0){
                memory [*operationCode] = 20 * 100 + *operand;
            }
            else if(strcmp(s,"PRNT") ==0){
                memory [*operationCode] = 12 * 100 + *operand;
            }
            else if(strcmp(s,"STOR") ==0){
                memory [*operationCode] = 21 * 100 + *operand;
            }
            else if(strcmp(s,"SET") ==0){
                memory [*operationCode] = *operand;
            }
            else if(strcmp(s,"ADD") ==0){
                memory [*operationCode] = 30 * 100 + *operand;
            }
            else if(strcmp(s,"SUB") ==0){
                memory [*operationCode] = 31 * 100 + *operand;
            }
            else if(strcmp(s,"DIV") ==0){
                memory [*operationCode] = 32 * 100 + *operand;
            }
            else if(strcmp(s,"MULT") ==0){
                memory [*operationCode] = 33 * 100 + *operand;
            }
            else if(strcmp(s,"MOD") ==0){
               memory [*operationCode] = 34 * 100 + *operand;
            }
            else if(strcmp(s,"BRAN") ==0){
            memory [*operationCode] = 40 * 100 + *operand;
            }
            else if(strcmp(s,"BRNG") ==0){
            memory [*operationCode] = 41 * 100 + *operand;
            }
            else if(strcmp(s,"BRZR") ==0){
            memory [*operationCode] = 42 * 100 + *operand;;
            }
            else if(strcmp(s,"HALT")==0){
            memory [*operationCode] =9999;
            }

        else {   /* Prints back to the user that the compiler did not recognize one of them commands as it was going through it */
            printf ("This is an unknown command, commands are case sensitive, program will now exit \n");
            exit(0);

        }
    }
    else{    /* Returns improper format if instructionRegister does not match 3*/
        printf("%d",*instructionRegister);
        printf("Improper Format, program will now exit \n");
        exit(0);
    }


}
/* Checks if the instruction data contains a HALT, if not it would terminate */
while(*instructionCounter<100){
    if (memory[*instructionCounter] == 9999){
        return 1;
    }
    else
        (*instructionCounter)++;
}
printf("Halt was not found, program will now exit");
exit (0); }

输入流看起来像这样:(就像我上面提到的,这个流被重定向到VIM命令,它欺骗程序从文件中读取而不必实际实现文件指针)

01 READ 20
02 LOAD 20
03 BRNG 12
04 STOR 30
05 LOAD 60
06 ADD 30
07 STOR 60
08 LOAD 50
09 ADD 70
10 STOR 70
11 BRAN 01
12 LOAD 60
13 DIV 70
14 STOR 80
15 WRIT 80
16 HALT 99
50 SET 1
60 SET 0
70 SET 0

我已经添加了一个printf,打印出正在扫描的内容到编译过程,这是我在输出中获得的,第一个参数是operationCode一个整数指针,第二个参数一个是字符串缓冲区s,第三个参数参数是操作数,它是一个整数指针):

1 READ 20
2 LOAD 20
3 BRNG 12
4 STOR 30
5 LOAD 60
6 ADD 30
7 STOR 60
8 LOAD 50
9 ADD 70
10 STOR 70
11 BRAN 1
12 LOAD 60
13 DIV 70
14 STOR 80
15 WRIT 80
16 HALT 99
50 SET 1
60 SET 0
70 SET 0
Word Overflow at memory element 70
 program will exit

我已经摸不着头几个小时了,我无法弄清楚为什么会这样做,因为我说我是C编程的新手,我仍然不知道如何调试和做C的东西,我来自Java背景。

编辑1:用户没有将程序写入虚拟计算机,程序已经编写,用户只需通过VIM中的命令重定向STDIN,如(./computer < prog1)(./computer < prog2)。程序应该成功编译,然后当计算机确定它是什么类型的程序时,它将根据它的任务提示用户输入。所以它可以要求用户输入值,然后它会计算它们的平均值,如果那是重定向到它的程序。

0 个答案:

没有答案