[英]Error: “Too many open files” …but why?
I have reduced my code down to this to try to isolate the problem. 我将代码简化为这样,以试图找出问题所在。 Basically what is happening is that if I try to use fopen() after the function call to createInstTable() then the file open is unsuccessful and I get an error that says "Too many open files".
基本上发生的事情是,如果我在函数调用createInstTable()之后尝试使用fopen(),则打开文件失败,并且出现错误消息“打开的文件太多”。 The program and the text file I try to open are in the same folder.
我尝试打开的程序和文本文件在同一文件夹中。 I know the problem lies in the createInstTable() function because if I comment it out then the file opens just fine.
我知道问题出在createInstTable()函数中,因为如果我注释掉它,那么文件打开就很好了。 If I call createInstTable() after I use fopen() then the file will open successfully but when I try to read from it then it messes up.
如果我在使用fopen()之后调用createInstTable(),则文件将成功打开,但是当我尝试从中读取文件时,文件会混乱。 Can someone figure out what is up with the createInstTable() function that might be giving me this error?
有人可以弄清楚createInstTable()函数到底是什么给我这个错误吗? Thanks in advance!
提前致谢! Here is the code:
这是代码:
// 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
and the output I get is: 我得到的输出是:
MULR 98 2 DIV 24 3 Error: failed to open.: Too many open files MULR 98 2 DIV 24 3错误:无法打开。:打开的文件太多
The main reason for weird behavior of your code is most likely the buffer overflow that the createInstTable
function causes. 代码异常行为的主要原因很可能是
createInstTable
函数导致的缓冲区溢出。 SIZE
, which is the number of entries in your array is 50 but you modify 59 entries. SIZE
,即数组中的条目数为50,但您修改了59个条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.