0
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "tempfile.h"
/* #include "writeinstance.h"*/

void tempfile(int m, int n, int t1,int t2, int k);          
int writeinstance(int m,int n,int t1, int t2,int k);
int main(void)
{
  int m = 20;int n = 200;int t1 = 1;int t2 = 100; int k = 100;
  int a,b,c,d;
  for (a = 1;a <= m;a++)
    {
      for (b = 10;b<=n;b+=10)
    {
      for (c = 10;c<=t2;c+=10)
        {

          tempfile(a,b,t1,c,k);
          printf("meow");

        }
    }
    }

   for (a = 1;a <= m;a++)
    {
      for (b = 1;b<=n;b+=10)
    {
      for (c = 1;c<=t2;c+=10)
        {
          for (d =1;d<=k;d++)
        {
          writeinstance(a,b,t1,c,d);
          printf("bark");
        }
        }
    }

    }
   return 0;
}


void tempfile(int m, int n, int t1,int t2, int k) 
{
  FILE* tempfile;

  char tempmatrix[100]; 
  int rand(void);

  void srand(unsigned int seed); /*set default value*/
  sprintf(tempmatrix, "tempfile%d_%d_%d_%d.txt",m,n,t2,k);
  tempfile = fopen(tempmatrix, "w");
  int i = 0;
  while(i<k) {
    srand(time(NULL));
    int j = 0;

    for (j = 0; j < k; j++) {

      int job[n][3];
      int a,b;
      fprintf(tempfile,"%d",m);
      fprintf(tempfile, "\n");
      fprintf(tempfile,"%d",n);
      fprintf(tempfile,"\n");



      for (a = 0 ; a < n ; a++) { /*generate random job time between t_min and t_max, to include both max value, it must add one(otherwise it will not include)*/
    for (b = 0;b < 3 ; b++ ) {
      job[a][b] = rand() % (t2 - t1 + 1) + t1;


      fprintf(tempfile,"%d ",job[a][b]);
    }
    fprintf(tempfile,"\n");
      }
      i++;
    }


  }
  fclose(tempfile);    

  return;
}


int writeinstance(int m,int n,int t1, int t2,int k) {
  FILE *ptr_readfile;
  FILE *ptr_writefile;
  char line [128];
  char tempfile[128];
  char fileoutputname[1000];
  int filecounter=1, linecounter=1;

  sprintf(tempfile, "tempfile%d_%d_%d_%d.txt",m,n,t2,k);
  ptr_readfile = fopen(tempfile,"r");
  if (!ptr_readfile)
    return 1;

  sprintf(fileoutputname, "instance%d_%d_%d_%d_%d.txt",m,n,t1,t2,filecounter); /*m(num_machine) n(num_jobs) t1 t2 instance*/
  ptr_writefile = fopen(fileoutputname, "w");

  while (fgets(line, sizeof line, ptr_readfile)!=NULL && filecounter <= k ) {
    if (linecounter == ((2+n)+1)) {
      fclose(ptr_writefile);
      linecounter = 1;
      filecounter++;
      sprintf(fileoutputname, "instance%d_%d_%d_%d_%d.txt",m,n,t1,t2,filecounter); /*m(num_machine) n(num_jobs) t1 t2 instance*/
      ptr_writefile = fopen(fileoutputname, "w");
      if (!ptr_writefile)
    return 1;
    }
    fprintf(ptr_writefile,"%s", line);
    linecounter++;
  }
  fclose(ptr_readfile);
  return 0;
}

Basically I just want to create m X n X t2 X K(20*200*100*100 = 400000)numbers of text file, and in each text file, there are m(first line),n(second line) and K matrixs with size 3 X n after it. In tempfile function , I just tried to print all the numbers I need in the text file. In writeinstance function, it just split the textfile. My code worked well if integer m,n,t1,t2 and k are small. However, if I just increase these numbers, It will just give me core dumped error(and it won't create all the textfiles I need) I have no idea how to fix this problem, maybe I should use tempfile? but if I use tempfile, how to determine its name? can anyone give me some suggestions? Thank you!

MeiLin Xu
  • 33
  • 4
  • You may possibly copy the core dump also here. – Soumya Kanti Dec 01 '18 at 07:53
  • 1
    A core dump can be caused by many different errors. You should run your program in a debugger, which will show you exactly where the error occurs. (I ran your program on my computer, and it worked without problems. It created 4000 temp files, not 400000.) – Thomas Padron-McCarthy Dec 01 '18 at 08:10
  • 1
    Have you looked at [Why is number of open files limited in Linux?](https://unix.stackexchange.com/questions/36841/why-is-number-of-open-files-limited-in-linux) or [Maximum number of files that can be opened by c “fopen” in linux](https://stackoverflow.com/questions/17931583/maximum-number-of-files-that-can-be-opened-by-c-fopen-in-linux) You don't specify your OS -- there would be similar posts for windows as well. See [File Handling - MSDN - Microsoft](https://msdn.microsoft.com/en-us/library/kdfaxaay.aspx) – David C. Rankin Dec 01 '18 at 08:16
  • Here is my hint - instead of calling fprintf 4 times like this fprintf(tempfile,"%d",m); fprintf(tempfile, "\n"); fprintf(tempfile,"%d",n); fprintf(tempfile,"\n"); I just 1 time would be better for reading the code. fprintf(tempfile,"%d\n%d\n",m, n); – Krassi Em Dec 01 '18 at 17:05
  • Here is one error in the writeinstance function. You will jump out of it immediately if ptr_writefile is Null. if (!ptr_writefile) return 1; However, you must first call fclose(ptr_readfile); and then return 1; Otherwise you can run out of available files to open in your program?! – Krassi Em Dec 01 '18 at 17:16
  • uh...you are right, there would only be 4000 temp files, but after that, they will be splited into 4000x100 = 400k final files. – MeiLin Xu Dec 01 '18 at 19:43
  • After I inceased my max files to 500k, I did create 4k temp files, but they won't split to 400k final files...what should I do...? – MeiLin Xu Dec 01 '18 at 20:05
  • You need to show the actual code that you are running now. Otherwise we'll just be guessing. – Thomas Padron-McCarthy Dec 02 '18 at 10:10

0 Answers0