我已将代码缩减到此以尝试隔离问题。基本上发生的事情是,如果我在函数调用createInstTable()之后尝试使用fopen(),那么打开的文件不成功,我收到一条错误,上面写着“打开文件过多”。我尝试打开的程序和文本文件位于同一文件夹中。我知道问题在于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] = '\0'; strcpy(inst[i].opCode, "98"); inst[i].opCode[2] = '\0'; 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] = '\0'; strcpy(inst[i].opCode, "DC"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "AND"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "40"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LPS"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "D0"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIXR"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "B8"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "SUBF"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "5C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDX"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "04"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SVC"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "B0"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STT"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "84"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIX"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "2C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "FLOAT"); inst[i].instruction[5] = '\0'; strcpy(inst[i].opCode, "C0"); inst[i].opCode[2] = '\0'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "LDT"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "74"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STA"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "0C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SHIFTR"); inst[i].instruction[6] = '\0'; strcpy(inst[i].opCode, "A8"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STB"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "78"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SIO"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "F0"); inst[i].opCode[2] = '\0'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "LDA"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "00"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "HIO"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "F4"); inst[i].opCode[2] = '\0'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "DIVF"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "64"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDCH"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "50"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JEQ"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "30"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SSK"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "EC"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDS"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "6C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "J"); inst[i].instruction[1] = '\0'; strcpy(inst[i].opCode, "3C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SUB"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "1C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RD"); inst[i].instruction[2] = '\0'; strcpy(inst[i].opCode, "D8"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDB"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "68"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RSUB"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "4C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "MULF"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "60"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JSUB"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "48"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "SUBR"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "94"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "DIVR"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "9C"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "LDL"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "08"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STSW"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "E8"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "COMPF"); inst[i].instruction[5] = '\0'; strcpy(inst[i].opCode, "88"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TIO"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "F8"); inst[i].opCode[2] = '\0'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "JLT"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "38"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "MUL"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "20"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "OR"); inst[i].instruction[2] = '\0'; strcpy(inst[i].opCode, "44"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "COMP"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "28"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "TD"); inst[i].instruction[2] = '\0'; strcpy(inst[i].opCode, "E0"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STS"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "7C"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "LDF"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "70"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "ADD"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "18"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "FIX"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "C4"); inst[i].opCode[2] = '\0'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "NORM"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "C8"); inst[i].opCode[2] = '\0'; inst[i].format = 1; i++;
strcpy(inst[i].instruction, "STF"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "80"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "CLEAR"); inst[i].instruction[5] = '\0'; strcpy(inst[i].opCode, "B4"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "ADDF"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "58"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STCH"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "54"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "STX"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "10"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "RMO"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "AC"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "COMPR"); inst[i].instruction[5] = '\0'; strcpy(inst[i].opCode, "A0"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "SHIFTL"); inst[i].instruction[6] = '\0'; strcpy(inst[i].opCode, "A4"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STL"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "14"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "ADDR"); inst[i].instruction[4] = '\0'; strcpy(inst[i].opCode, "90"); inst[i].opCode[2] = '\0'; inst[i].format = 2; i++;
strcpy(inst[i].instruction, "STI"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "D4"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "JGT"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "34"); inst[i].opCode[2] = '\0'; inst[i].format = 3; i++;
strcpy(inst[i].instruction, "DIV"); inst[i].instruction[3] = '\0'; strcpy(inst[i].opCode, "24"); inst[i].opCode[2] = '\0'; 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 错误:无法打开:打开文件太多
答案 0 :(得分:4)
代码奇怪行为的主要原因很可能是createInstTable
函数导致的缓冲区溢出。 SIZE
,您的数组中的条目数 50 ,但您修改 59 条目。