点击蓝字
关注我们
DRAGON BOAT FESTIVAL
练习35
01 要求:
使用命令行参数向文件中输入随机数据
例如命令行中输入:
源.exe data.txt 0 100 5
则可在data.txt 文件中获得5个[0,100]之间的数据
其中源.exe为源程序生成的可执行文件
02 程序示例如下:
/*使用命令行参数向文件中输入随机数据
运行:源.exe data.txt 0 100 5
运行文件 数据文件 随机数最小值 随机数最大值 n个随机数
*/
int main(int argc, char *argv[])
{
FILE *fp1;
fp1 = fopen(argv[1], "w");
int min = atoi(argv[2]);
int max = atoi(argv[3]);
int n = atoi(argv[4]);
int data;
srand((unsigned)time(NULL));
//初始化随机数
for (int i = 0; i < n; i++){
data = rand() % (max - min + 1) + min;
fprintf(fp1, "%d\n", data);
//向所建文件写一字符串
}
return 0;
}
03:输出:
输入的命令行参数为:
源.exe data.txt 0 100 5
结果为
7
78
55
18
90
球分享
球点赞
球在看