写入具有不同文件名的单独文件 [英] Write to separate files with different file names

查看:72
本文介绍了写入具有不同文件名的单独文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要实现的目标:


假设用户输入的是:

生成随机实例...

输入电路板尺寸MAX_X MAX_Y:100200

Enter the circuit board size MAX_X MAX_Y: 100 200

输入点数NUM_PT:10

Enter the number of points NUM_PT: 10

输入要生成的随机实例数:7

Enter the number of random instances to be generated: 7

您的程序将总共生成7个实例,分别写入7个
文件 instance10_j.txt,对于j = 1,2,3,...每个
实例具有矩形区域[0; 100] X [0; 200],并有10个
积分。在矩形区域内均匀地随机生成点的坐标
。并且您的程序确保每个实例中没有
个重复点。如果
程序无法生成这些文件,则打印出错误并退出。
所有这些文件都执行
命令保存在当前目录中,并且程序将显示在屏幕上:

your program will generate in total 7 instances, written into 7 separate files "instance10_j.txt", for j = 1, 2, 3, ... Each instance has the rectangular area [0 ; 100] X [0 ; 200], and has 10 points. The coordinates of a point is generated uniformly randomly within the rectangular area. And your program makes sure there are no duplicate points within each instance. If it is impossible for your program to generate these files, prints out what an error is and quits. All these files are saved in the current directory executing the command, and your program prints to the screen:

instance10_1.txt生成

instance10_1.txt generated

instance10_2.txt生成

instance10_2.txt generated

instance10_3.txt生成

instance10_3.txt generated

instance10_4.txt生成

instance10_4.txt generated

生成instance10_5.txt

instance10_5.txt generated

instance10_6.txt生成

instance10_6.txt generated

instance10_7.txt生成...完成!

instance10_7.txt generated ... done!

这是我到目前为止所做的:

int writetofile(max_X, max_Y, numpt, random_inst);
int main(int argc, char *argv[]) 

{

  FILE *fp;

  int max_x, max_y, num_pt, rand_inst;
  int *x_coordinate, *y_coordinate;

  int inputfile = 0, outputfile = 0;
  int i;

  if (argc == 1)
    {
      /* to generate random instances, accepting parameters from stdin */
      printf("Generating random instances...");
      printf("Enter the circuit board size MAX_X MAX_Y:  ");
      scanf("%d %d", &max_x, &max_y);
      printf("Enter the number of points NUM_PT:  ");
      scanf("%d", &num_pt);
      printf("Enter the number of random instances to be generated:  ");
      scanf("%d", &rand_inst);
      return 1;
    }  
       /* MAIN FUNCTION CONTINUES FOR REMAINING WORK */
}

int writetofile(max_X, max_Y, numpt, random_inst)

{

  FILE *fp;
  int i;

  for (i = 1; i <= random_inst; i++)
    {
      /* NEED HELP HERE */
      fp = fopen(File with name instance[num_pt]_[rand_inst], "w");

      fprintf(fp, "#%s\n", argv[inputfile]);
      fprintf(fp, "#area [0, MAX_X] x [0, MAX_Y]\n");
      fprintf(fp, "%d\t%d\n", max_x, max_y);
      fprintf(fp, "#number of points NUM_PT\n");
      fprintf(fp, "%d\n", num_pt);
      fprintf(fp, "#coordinates\n");
      for (i = 0; i < num_pt; i++) 
      {
          fprintf(fp, "%d\t%d\n", x_coordinate[i], y_coordinate[i]);
      }
    fprintf(fp, "#end of instance\n");
    fclose(fp);

我需要创建没有重复的随机实例,但更重要的是,我应该将它们写到单独的文件中

I need to create random instances with no duplication but more importantly I should write them to separate files

我的困难是打开一个名为 instance [num_pt] _ [random_instances] 的文件,我认为应该将其合并到for循环中。

My difficulty is in opening a file which would be named as instance[num_pt]_[random_instances], which I think should be incorporated in the for loop.

我正在使用Ubuntu终端通过ssh访问我的实验室计算机。

I am using Ubuntu terminal to access my lab computer via ssh.

语言:c99;编译器:gcc

推荐答案

就像提到的 Kaylum 一样。

char name[MAX_LEN];

/* Incorporate this into your for loop */

snprintf(name, MAX_LEN, "instance%d_%d.txt", num_pt, random_inst);
fopen(name, "w");

这篇关于写入具有不同文件名的单独文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆