【发布时间】:2017-10-02 02:46:57
【问题描述】:
代码的要点是提示用户输入 max_x、max_y 值、点数和要创建的文件总数。从这里创建一个文件,文件中输入的代码可以正常工作。
这就是我想要达到的目标:
假设用户输入是:
生成随机实例 ...
输入电路板尺寸MAX_X MAX_Y: 100 200
输入点数NUM_PT:10
输入要生成的随机实例数:7
我被卡住的地方是程序不会迭代 7 次,而是只创建一个名为 instance7_007.txt 的文件
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
FILE *fp;
int max_x, max_y, num_pt, random_inst;
int MAX_LEN = 200;
int *x_coordinate, *y_coordinate;
int inputfile = 0, outputfile = 0;
int i;
char name[MAX_LEN];
if (argc == 1) {
/* to generate random instances, accepting parameters from stdin */
printf("Generating random instance . . .\n");
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", &random_inst);
for (i=1; i < random_inst; i++);
{
snprintf(name, MAX_LEN, "instance%d_%03d.txt", num_pt, i);
fp = fopen(name, "w");
printf("%s generated", &name);
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, "%d\n", num_pt);
fprintf(fp, "#coordinates\n");
for (i=0; i < num_pt; i++)
{
fprintf(fp, "%d\t%d\n", rand()%max_x, rand()%max_y);
}
fprintf(fp, "#end of instance\n");
fclose(fp);
}
return 1;
}
}
【问题讨论】:
-
也没有出现编译错误,所以我很茫然。
-
请edit你的问题告诉我们你做了什么样的调试。我希望您已经在 Valgrind 或类似的检查器中运行了您的minimal reproducible example,并使用诸如 GDB 之类的调试器进行了调查。确保您也启用了全套编译器警告。这些工具告诉了你什么,它们缺少什么信息?并阅读 Eric Lippert 的 How to debug small programs。
标签: c