Error: "Too many open files" ...but why?
Error: "Too many open files" ...but why?
我已将我的代码缩减为这样以尝试找出问题所在。基本上发生的事情是,如果我在函数调用 createInstTable() 之后尝试使用 fopen(),则文件打开失败,并且我收到一条错误消息 "Too many open files"。我尝试打开的程序和文本文件位于同一个文件夹中。我知道问题出在 createInstTable() 函数中,因为如果我将其注释掉,那么文件就可以正常打开。如果我在使用 fopen() 之后调用 createInstTable() ,那么文件将成功打开,但是当我尝试从中读取时,它就会出错。有人能弄清楚可能会给我这个错误的 createInstTable() 函数是怎么回事吗?提前致谢!这是代码:
// Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Defines
#define SIZE 50
#define BUFLEN 81
// Structs
typedef struct inopform{
char instruction[8];
char opCode[3];
int format;
} inopform;
// Prototypes
void createInstTable(inopform* inst);
// main
int main(int argc, char* argv[]) {
inopform* inst = (inopform*)calloc(SIZE, sizeof(inopform));
createInstTable(inst);
FILE *ifp = fopen(argv[1], "r");
if (ifp == NULL)
perror("Error: failed to open.");
else
printf ("\n Successfully opened file\n\n");
fclose(ifp);
return 0;
} // end main
// fills the instruction table with the instruction names, corresponding op codes and formats
void createInstTable(inopform* inst) {
int i = 0;
strcpy(inst[i].instruction, "MULR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "98"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; printf("%s\t%s\t%d\n", inst[i].instruction, inst[i].opCode, inst[i].format); i++;
strcpy(inst[i].instruction, "WD"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "DC"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "AND"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "40"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LPS"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "D0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIXR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "B8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "SUBF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "5C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "04"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SVC"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "B0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "84"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "2C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "FLOAT"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "C0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "LDT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "74"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STA"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "0C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SHIFTR"); inst[i].instruction[6] = '[=10=]'; strcpy(inst[i].opCode, "A8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STB"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "78"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SIO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "F0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "LDA"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "00"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "HIO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "F4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "DIVF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "64"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDCH"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "50"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JEQ"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "30"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SSK"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "EC"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDS"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "6C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "J"); inst[i].instruction[1] = '[=10=]'; strcpy(inst[i].opCode, "3C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SUB"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "1C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RD"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "D8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDB"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "68"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RSUB"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "4C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "MULF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "60"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JSUB"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "48"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SUBR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "94"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "DIVR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "9C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "LDL"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "08"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STSW"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "E8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "COMPF"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "88"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "F8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "JLT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "38"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "MUL"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "20"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "OR"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "44"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "COMP"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "28"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TD"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "E0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STS"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "7C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDF"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "70"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "ADD"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "18"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "FIX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "C4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "NORM"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "C8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "STF"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "80"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "CLEAR"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "B4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "ADDF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "58"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STCH"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "54"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "10"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RMO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "AC"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "COMPR"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "A0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "SHIFTL"); inst[i].instruction[6] = '[=10=]'; strcpy(inst[i].opCode, "A4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STL"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "14"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "ADDR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "90"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STI"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "D4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JGT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "34"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "DIV"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "24"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; printf("%s\t%s\t%d\n", inst[i].instruction, inst[i].opCode, inst[i].format);
return;
} // end createInstTable
我得到的输出是:
MULR 98 2
DIV 24 3
错误:无法打开。:打开的文件太多
您的代码出现奇怪行为的主要原因很可能是 createInstTable
函数导致的缓冲区溢出。 SIZE
,也就是你数组中的条目数是50,但是你修改了59个条目。
我已将我的代码缩减为这样以尝试找出问题所在。基本上发生的事情是,如果我在函数调用 createInstTable() 之后尝试使用 fopen(),则文件打开失败,并且我收到一条错误消息 "Too many open files"。我尝试打开的程序和文本文件位于同一个文件夹中。我知道问题出在 createInstTable() 函数中,因为如果我将其注释掉,那么文件就可以正常打开。如果我在使用 fopen() 之后调用 createInstTable() ,那么文件将成功打开,但是当我尝试从中读取时,它就会出错。有人能弄清楚可能会给我这个错误的 createInstTable() 函数是怎么回事吗?提前致谢!这是代码:
// Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Defines
#define SIZE 50
#define BUFLEN 81
// Structs
typedef struct inopform{
char instruction[8];
char opCode[3];
int format;
} inopform;
// Prototypes
void createInstTable(inopform* inst);
// main
int main(int argc, char* argv[]) {
inopform* inst = (inopform*)calloc(SIZE, sizeof(inopform));
createInstTable(inst);
FILE *ifp = fopen(argv[1], "r");
if (ifp == NULL)
perror("Error: failed to open.");
else
printf ("\n Successfully opened file\n\n");
fclose(ifp);
return 0;
} // end main
// fills the instruction table with the instruction names, corresponding op codes and formats
void createInstTable(inopform* inst) {
int i = 0;
strcpy(inst[i].instruction, "MULR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "98"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; printf("%s\t%s\t%d\n", inst[i].instruction, inst[i].opCode, inst[i].format); i++;
strcpy(inst[i].instruction, "WD"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "DC"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "AND"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "40"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LPS"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "D0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIXR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "B8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "SUBF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "5C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "04"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SVC"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "B0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "84"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "2C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "FLOAT"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "C0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "LDT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "74"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STA"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "0C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SHIFTR"); inst[i].instruction[6] = '[=10=]'; strcpy(inst[i].opCode, "A8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STB"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "78"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SIO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "F0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "LDA"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "00"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "HIO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "F4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "DIVF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "64"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDCH"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "50"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JEQ"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "30"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SSK"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "EC"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDS"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "6C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "J"); inst[i].instruction[1] = '[=10=]'; strcpy(inst[i].opCode, "3C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SUB"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "1C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RD"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "D8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDB"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "68"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RSUB"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "4C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "MULF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "60"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JSUB"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "48"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SUBR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "94"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "DIVR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "9C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "LDL"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "08"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STSW"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "E8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "COMPF"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "88"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "F8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "JLT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "38"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "MUL"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "20"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "OR"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "44"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "COMP"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "28"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TD"); inst[i].instruction[2] = '[=10=]'; strcpy(inst[i].opCode, "E0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STS"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "7C"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDF"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "70"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "ADD"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "18"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "FIX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "C4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "NORM"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "C8"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "STF"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "80"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "CLEAR"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "B4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "ADDF"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "58"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STCH"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "54"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STX"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "10"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RMO"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "AC"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "COMPR"); inst[i].instruction[5] = '[=10=]'; strcpy(inst[i].opCode, "A0"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "SHIFTL"); inst[i].instruction[6] = '[=10=]'; strcpy(inst[i].opCode, "A4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STL"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "14"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "ADDR"); inst[i].instruction[4] = '[=10=]'; strcpy(inst[i].opCode, "90"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STI"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "D4"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JGT"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "34"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "DIV"); inst[i].instruction[3] = '[=10=]'; strcpy(inst[i].opCode, "24"); inst[i].opCode[2] = '[=10=]'; inst[i].format = 3; printf("%s\t%s\t%d\n", inst[i].instruction, inst[i].opCode, inst[i].format);
return;
} // end createInstTable
我得到的输出是:
MULR 98 2 DIV 24 3 错误:无法打开。:打开的文件太多
您的代码出现奇怪行为的主要原因很可能是 createInstTable
函数导致的缓冲区溢出。 SIZE
,也就是你数组中的条目数是50,但是你修改了59个条目。