問題を切り分けるために、コードをこれまで減らしました。基本的に何が起こっているのかというと、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エラー:開くことができませんでした。:開いているファイルが多すぎます
コードの奇妙な動作の主な理由は、createInstTable
関数が引き起こすバッファオーバーフローである可能性があります。SIZE
、これは配列内のエントリ数が50ですが、59エントリを変更します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加