#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!