title: Error: "Too many open files" ...but why?
body: <p>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". 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. 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. Can someone figure out what is up with the createInstTable() function that might be giving me this error? Thanks in advance! Here is the code: </p>
  
  <pre><code>// Libraries
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  
  // 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
  </code></pre>
  
  <p>and the output I get is: </p>
  
  <p>MULR    98      2
  DIV     24      3
  Error: failed to open.: Too many open files</p>