首页 > 解决方案 > 创建 400K 非空文本文件时是否可以避免核心转储?

问题描述

#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;
}

基本上我只想创建 m X n X t2 XK(20*200*100*100 = 400000) 个文本文件,在每个文本文件中,有 m(第一行)、n(第二行)和 K 个矩阵其后尺寸为 3 X n。在 tempfile 函数中,我只是尝试在文本文件中打印我需要的所有数字。在 writeinstance 函数中,它只是拆分文本文件。如果整数 m,n,t1,t2 和 k 很小,我的代码运行良好。但是,如果我只是增加这些数字,它只会给我核心转储错误(并且它不会创建我需要的所有文本文件)我不知道如何解决这个问题,也许我应该使用 tempfile?但是如果我使用 tempfile,如何确定它的名称?谁能给我一些建议?谢谢!

标签: carraysfiletemporary-files

解决方案


推荐阅读