int seq_gen(void){
FILE* fd=fopen("./1000.txt","w");
char* scope[]={"-1","0","1"};
srand((unsigned)time(NULL));
int i;
for( i=0;i<LEN;i++){
fprintf(fd,"%s\n",scope[rand()%3]);
}
fclose(fd);
return 0;
}
想问这段代码的意思,希望能一句句解释
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
cpp_learners 2021-03-13 16:43关注差不多是这个意思。。。
int seq_gen(void) { FILE* fd = fopen("./1000.txt", "w"); // 以写的方式打开1000.txt文件 //char* scope[] = { "-1","0","1" }; // 这样写有问题!!! char scope[] = { '-1', '0', '1' }; // 定义字符数组,将字符1、字符0、字符1存储 srand((unsigned)time(NULL)); // 随机函数,参数随机值 int i; for (i = 0; i < LEN; i++) { // 循环LEN次 fprintf(fd, "%s\n", scope[rand() % 3]); // 将scope字符数组中的字符按照 随机函数产生的 随机值 % 3 得到的索引,取得的字符写入文件中。 } fclose(fd); // 关闭文件 return 0; }解决 无用评论 打赏 举报